简体   繁体   English

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

[英]use ngif to hide a row in mat-table

I am trying to hide a row after I click on a button in the mat-table. 我点击mat-table中的按钮后试图隐藏一行。 I don't know where to put *ngIf . 我不知道在哪里放*ngIf I tried on ng-container but it does not work. 我尝试过ng-container但它不起作用。 Below is my HTML file. 下面是我的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>

The other way is to add class to mat-row and use CSS to hide it. 另一种方法是将类添加到mat-row并使用CSS来隐藏它。

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

You can look also on similar example from Angualr Material site: https://stackblitz.com/angular/voqbanbobpa?file=app%2Ftable-expandable-rows-example.ts 您还可以查看Angualr Material网站上的类似示例: https ://stackblitz.com/angular/voqbanbobpa?file = app%2Ftable-expandable-rows-example.ts

You need to modify your dataSource object from the component code, application in your case, and remove the row you want to hide. 您需要从组件代码,案例中的application修改dataSource对象,并删除要隐藏的行。

It depends on your component code, but you will most likely do: 这取决于您的组件代码,但您很可能会这样做:

this.application = this.application.splice(i, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM