如何在Angular中将按钮放置到mat-table

时间:2018-12-04 16:05:33

标签: javascript html angular mat-table

我想在Angular中的mat-table上放置一个删除按钮或Angular垃圾图标。我该如何实现? 我的工作表代码:

<mat-table #table [dataSource]="ELEMENT_DATA">
  <ng-container cdkColumnDef="position">
    <mat-header-cell *cdkHeaderCellDef fxFlex="40%">Position</mat-header-cell>
    <mat-cell *cdkCellDef="let config" fxFlex="40%">{{config.position}}</mat-cell>
  </ng-container>

  <ng-container cdkColumnDef="name">
    <mat-header-cell *cdkHeaderCellDef fxFlex="30%">Label</mat-header-cell>
    <mat-cell *cdkCellDef="let config" fxFlex="30%">{{config.name}}</mat-cell>
  </ng-container>

  <ng-container cdkColumnDef="weight">
    <mat-header-cell *cdkHeaderCellDef fxFlex="10%">Order</mat-header-cell>
    <mat-cell *cdkCellDef="let config" fxFlex="10%">{{config.weight}}</mat-cell>
  </ng-container>

  <ng-container cdkColumnDef="symbol">
    <mat-header-cell *cdkHeaderCellDef fxFlex="10%">Symbol</mat-header-cell>
    <mat-cell *cdkCellDef="let config" fxFlex="10%">{{config.symbol}}</mat-cell>
  </ng-container>

  <mat-header-row *cdkHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *cdkRowDef="let config; columns: displayedColumns;"
           (click)="editConfig(config.id)"></mat-row>

2 个答案:

答案 0 :(得分:2)

将此按钮添加到另一个Mat-cell。

<button class="btn btn-outline-secondary" (click)="delete()"> <i class="fas fa-trash-alt"></i></button>

答案 1 :(得分:0)

为此,您需要一个额外的列,例如:

displayedColumns: string[] = ['position', 'name', 'weight', 'symbol','deleteAction'];

并在Mat-Table中使用:

<ng-container cdkColumnDef="deleteAction">
    <th mat-header-cell *matHeaderCellDef> Delete </th>
      <td mat-cell *matCellDef="let element"><i class="material-icons" (click)="delete(element)">delete</i>
</ng-container> 

有效的StackBlitz Example