Material Table remove button with strike through

January 11, 2023 · 1 min readLucian Ciobanu
Tags:AngularAngularMaterial

Today I had a lot of fun preparing a Remove button with the related logic from a material table datasource as follows:

table.component.html

<mat-table [dataSource]="dataSource matSort [hidden]="dataSource.data.length === 0" >
    <ng-container matColumnDef="Name">
         <mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
         <mat-cell *matCellDef="let element" [ngClass]="{'strike': element.toBeDeleted}">{{ element.name }}</mat-cell>
    </ng-container>
    <ng-container matColumnDef="Actions">
         <mat-header-cell *matHeaderCell></mat-header-cell>
         <mat-cell *matCellDef="let element">
              <button mat-button target="_blank" (click)="removeSite(element)">{{element.ToBeDeleted === true ? 'Undo remove' : 'Remove' }}</button>
         </mat-cell>

table.component.scss

.strike {
  text-decoration: line-through;
}

table.component.ts

...
public removeElement(element) {
  const elementInGroup = this.dataSource.find(e => e.Name === element.Name);
  elementInGroup.ToBeDeleted = !elementInGroup.ToBeDeleted;
}

I really enjoyed this implementation.

Hope this helped!

Cheers,
Lucian

Picture of author

Web Development & Software Delivery