Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a single row from the table on clicking the icon/button

I am using Table with pagination component in my project.I need to delete the single row at a time on clicking the delete icon. Here is the stackblitz link i have created.

Tried this link,here they have performed edit | update | delete operations.I want only delete operation.How can i achieve this?

like image 395
PGH Avatar asked Dec 14 '22 15:12

PGH


1 Answers

Create a delete function like so:

delete(elm) {
  this.dataSource.data = this.dataSource.data.filter(i => i !== elm)
}

And call it from the HTML:

              <ng-container matColumnDef="columndelete">
                <th style="width:15%;" mat-header-cell *matHeaderCellDef> </th>
                <td  mat-cell *matCellDef="let element">       
                  <mat-icon (click)="delete(element)">delete</mat-icon> </td>
              </ng-container>

Here is a fork of the StackBlitz

EDIT

If you don't need to udpate the value of position, you can use:

<td mat-cell *matCellDef="let element; let idx = index"> {{ idx + 1 }} 

On the cell. This will display the correct value, but leave position unchanged.

If you want to change the position value as well, you can use:

   this.dataSource.data = this.dataSource.data
      .filter(i => i !== elm)
      .map((i, idx) => (i.position = (idx + 1), i)); // Update the position
like image 194
user184994 Avatar answered Dec 28 '22 10:12

user184994