最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to show the area without chips if there is no value - Stack Overflow

programmeradmin1浏览0评论

My table uses chips to input for the value under "type". but when there is no values in it. there still is a empty chip in it. anyone knows how i can fix this problem?

[![html

 <ng-container matColumnDef="type">
      <th mat-header-cell *matHeaderCellDef mat-sort-header="type">Type</th>
      <td mat-cell *matCellDef="let truck" class="pr-24">
        <mat-chip-list>
          <span *ngFor="let truck of truck.type.split(',')">
            <mat-chip>{{truck}}</mat-chip>
          </span>
        </mat-chip-list>
      </td>
  </ng-container>][1]][1]

ponent.ts

import { Component, OnInit, ViewChild, ElementRef, Input, OnChanges } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource, MatDialog, MatChipsModule,  MatChipInputEvent } from '@angular/material';
import { SelectionModel } from '@angular/cdk/collections';
import { Truck } from '../truck';
import { MapsAPILoader } from '@agm/core';
import { TruckDetailComponent } from '../truck-detail/truck-detailponent';
import { PlannerProject } from 'app/services/planner-projects/planner-project';
import { TrucksService } from '../trucks.service';
import { map, debounceTime, distinctUntilChanged, tap } from 'rxjs/operators';
import * as _ from 'lodash';
import { fromEvent } from 'rxjs';
import { ConfirmComponent } from 'app/shared/ponents/confirm/confirmponent';
import { FuseConfirmDialogComponent } from '@fuse/ponents/confirm-dialog/confirm-dialogponent';
import { MyDialogComponent } from 'app/main/delivery-orders/my-dialog/my-dialogponent';
import {COMMA, ENTER} from '@angular/cdk/keycodes';


@Component({
  selector: 'app-trucks',
  templateUrl: './trucksponent.html',
  styleUrls: ['./trucksponent.scss']

})
export class TrucksComponent implements OnInit, OnChanges {
  @Input() project: PlannerProject;

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  selection: SelectionModel<Truck>;

  _displayColumns: string[] = ['selectCol', 'truckSize', 'truckBuildUp', 'truckName', 'type', 'address', 'shift', 'maxWeight', 'maxVolume', 'actions'];

  _dataSource: MatTableDataSource<Truck>;

  @ViewChild('search') search: ElementRef;
  projectData: string;

  constructor(private mapsLoader: MapsAPILoader,
              private _matDialog: MatDialog,
              private truckService: TrucksService) { }

So it should show an empty area in the row without chips if there is no data

My table uses chips to input for the value under "type". but when there is no values in it. there still is a empty chip in it. anyone knows how i can fix this problem?

[![html

 <ng-container matColumnDef="type">
      <th mat-header-cell *matHeaderCellDef mat-sort-header="type">Type</th>
      <td mat-cell *matCellDef="let truck" class="pr-24">
        <mat-chip-list>
          <span *ngFor="let truck of truck.type.split(',')">
            <mat-chip>{{truck}}</mat-chip>
          </span>
        </mat-chip-list>
      </td>
  </ng-container>][1]][1]

ponent.ts

import { Component, OnInit, ViewChild, ElementRef, Input, OnChanges } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource, MatDialog, MatChipsModule,  MatChipInputEvent } from '@angular/material';
import { SelectionModel } from '@angular/cdk/collections';
import { Truck } from '../truck';
import { MapsAPILoader } from '@agm/core';
import { TruckDetailComponent } from '../truck-detail/truck-detail.ponent';
import { PlannerProject } from 'app/services/planner-projects/planner-project';
import { TrucksService } from '../trucks.service';
import { map, debounceTime, distinctUntilChanged, tap } from 'rxjs/operators';
import * as _ from 'lodash';
import { fromEvent } from 'rxjs';
import { ConfirmComponent } from 'app/shared/ponents/confirm/confirm.ponent';
import { FuseConfirmDialogComponent } from '@fuse/ponents/confirm-dialog/confirm-dialog.ponent';
import { MyDialogComponent } from 'app/main/delivery-orders/my-dialog/my-dialog.ponent';
import {COMMA, ENTER} from '@angular/cdk/keycodes';


@Component({
  selector: 'app-trucks',
  templateUrl: './trucks.ponent.html',
  styleUrls: ['./trucks.ponent.scss']

})
export class TrucksComponent implements OnInit, OnChanges {
  @Input() project: PlannerProject;

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  selection: SelectionModel<Truck>;

  _displayColumns: string[] = ['selectCol', 'truckSize', 'truckBuildUp', 'truckName', 'type', 'address', 'shift', 'maxWeight', 'maxVolume', 'actions'];

  _dataSource: MatTableDataSource<Truck>;

  @ViewChild('search') search: ElementRef;
  projectData: string;

  constructor(private mapsLoader: MapsAPILoader,
              private _matDialog: MatDialog,
              private truckService: TrucksService) { }

So it should show an empty area in the row without chips if there is no data

Share Improve this question edited Apr 26, 2019 at 8:16 Kamil Naja 6,6926 gold badges37 silver badges52 bronze badges asked Apr 26, 2019 at 7:42 SoberdogK9SoberdogK9 1272 silver badges12 bronze badges 3
  • Use *ngIf="truck.type" in <mat-chip-list> – Raphael Mayer Commented Apr 26, 2019 at 7:47
  • split on an empty string will return an array with 1 item which is empty. Add a check to see if truck.type is empty string then not show – DTul Commented Apr 26, 2019 at 7:48
  • I highly remend to not reuse your variables like that. Use different variable names, not only "truck" for multiple meanings. – Raphael Mayer Commented Apr 26, 2019 at 7:58
Add a ment  | 

3 Answers 3

Reset to default 8

add an *ngIf to your chip, this will make it show only when the condition is satisfied inside the *ngIf.

<mat-chip *ngIf="truck">{{truck}}</mat-chip>

Array.split() on an empty string returns an array with 1 empty string. Add a check using *ngIf="truck.type" to your chip list to only render when it is not emtpy.

var truck = {type: '' };

var s = truck.type.split(',');

console.log(s); // See Array with one item.

I would check if truck.type is empty, because split will return an array of size 1 if it cannot split the string, therefore returning an empty string. This is the reason you get empty chips.

<mat-chip-list *ngIf="truck && truck.type && truck.type.length > 0">
  <span *ngFor="let truck of truck.type.split(',')">
    <mat-chip>{{truck}}</mat-chip>
  </span>
</mat-chip-list>

Edit: changed *ngIf check for truck and truck.type

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>