abidinberkay
abidinberkay

Reputation: 2025

How to put button to mat-table in Angular

I want to put a delete button or Angular trash icon to a mat-table in Angular. How can i achieve it? My working table code:

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

Upvotes: 7

Views: 7143

Answers (2)

Prashant Pimpale
Prashant Pimpale

Reputation: 10717

You need an extra column for that, for example:

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

and to use in 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> 

A working StackBlitz Example

Upvotes: 5

Juan Medina
Juan Medina

Reputation: 154

Add this button to another mat-cell.

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

Upvotes: 2

Related Questions