使用ngif在mat-table中隐藏一行

时间:2019-04-16 06:48:26

标签: angular mat-table

单击垫子表中的按钮后,我试图隐藏一行。我不知道将*ngIf放在哪里。我尝试了ng-container,但是它不起作用。下面是我的HTML文件。

<mat-table class="lessons-table mat-elevation-z8" [dataSource]="application">
  <ng-container matColumnDef="ID">
    <mat-header-cell *matHeaderCellDef>ID</mat-header-cell>
    <mat-cell *matCellDef="let application">{{application.id}}</mat-cell>
  </ng-container>

  <ng-container matColumnDef="Name">
    <mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
    <mat-cell *matCellDef="let application">{{application.name}}</mat-cell>
  </ng-container>

  <ng-container matColumnDef="ApplicationDate">
    <mat-header-cell *matHeaderCellDef>Application Date</mat-header-cell>
    <mat-cell *matCellDef="let application">{{application.applyDate}}</mat-cell>
  </ng-container>

  <ng-container matColumnDef="TrackingNumber">
    <mat-header-cell *matHeaderCellDef>Tracking Number</mat-header-cell>
    <mat-cell *matCellDef="let application">{{application.trackNumber}}</mat-cell>
  </ng-container>

  <ng-container matColumnDef="Operation">
    <mat-header-cell *matHeaderCellDef>Operation</mat-header-cell>
    <mat-cell *matCellDef="let application">
      <button mat-raised-button color="primary" (click)="onClickGrant()">Grant</button>&nbsp; &nbsp; &nbsp;
      <button mat-raised-button color="warn" (click)="onClickDeny()">Deny</button>
    </mat-cell>
  </ng-container>

  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>
<mat-paginator [pageSizeOptions]="[5,10,15,20]" [pageSize]="5" showFirstLastButtons></mat-paginator>

2 个答案:

答案 0 :(得分:2)

另一种方法是将类添加到mat-row并使用CSS隐藏它。

<mat-row *matRowDef="..." [class.hidden]="YOUR_IF_HERE"></mat-row>

您也可以在Angualr Material网站上查看类似的示例: https://stackblitz.com/angular/voqbanbobpa?file=app%2Ftable-expandable-rows-example.ts

答案 1 :(得分:1)

您需要通过组件代码<xarray.Dataset> Dimensions: (month: 12) Coordinates: * month (month) int64 1 2 3 4 5 6 7 8 9 10 11 12 Data variables: temp (month) float64 dask.array<shape=(12,), chunksize=(1,)> 来修改dataSource对象,并删除要隐藏的行。

这取决于您的组件代码,但是您很可能会这样做:

application