如果没有价值,如何显示没有筹码的区域

问题描述 投票:3回答:3

enter image description hereMy表使用芯片输入“type”下的值。但是当它没有价值时。里面还有一个空芯片。谁知道我怎么能解决这个问题?

[![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]

component.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.component';
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/components/confirm/confirm.component';
import { FuseConfirmDialogComponent } from '@fuse/components/confirm-dialog/confirm-dialog.component';
import { MyDialogComponent } from 'app/main/delivery-orders/my-dialog/my-dialog.component';
import {COMMA, ENTER} from '@angular/cdk/keycodes';


@Component({
  selector: 'app-trucks',
  templateUrl: './trucks.component.html',
  styleUrls: ['./trucks.component.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) { }

因此,如果没有数据,它应该在没有芯片的行中显示空白区域

javascript angular angular-template
3个回答
5
投票

在芯片上添加* ngIf,只有在* ngIf内满足条件时才会显示。

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

1
投票

空字符串上的Array.split()返回一个空字符串的数组。使用*ngIf="truck.type"将检查添加到您的芯片列表中,只有在它不是emtpy时才会呈现。

var truck = {type: '' };

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

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

1
投票

我会检查truck.type是否为空,因为如果它不能拆分字符串,split将返回一个大小为1的数组,因此返回一个空字符串。这就是你获得空芯片的原因。

<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>

编辑:更改* ngIf检查卡车和truck.type

© www.soinside.com 2019 - 2024. All rights reserved.