简体   繁体   中英

With Angular Material tables, how to have a click event handler for a table row that ignores one column

I have a row in my Angular Material table that I want to be clickable. The problem is that I also have an icon in the last column of the row that I also want to be clickable but handled differently. Right now, when I click on the icon, it calls both handlers and I don't want that. Here is my code:

 <div class="mat-elevation-z8"> <table mat-table #table [dataSource]="dataSource" matSort aria-label="Publisher"> <!-- LastName Column --> <ng-container matColumnDef="lastName"> <th mat-header-cell *matHeaderCellDef mat-sort-header>Last Name</th> <td mat-cell *matCellDef="let row">{{row.lastName}}</td> </ng-container> <!-- FirstName Column --> <ng-container matColumnDef="firstName"> <th mat-header-cell *matHeaderCellDef mat-sort-header>First Name</th> <td mat-cell *matCellDef="let row">{{row.firstName}}</td> </ng-container> <!-- Actions --> <ng-container matColumnDef="actions"> <th mat-header-cell *matHeaderCellDef></th> <td mat-cell *matCellDef="let row"> <button mat-icon-button (click)="onClickDelete(row.id)"> <mat-icon>delete</mat-icon> </button> </td> </ng-container> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-row (click)="onClickPublisher(row.id)" *matRowDef="let row; columns: displayedColumns;"></tr> </table> <mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator> </div> 

How can I make it so that when I click on the delete icon it doesn't call the click handler for the entire row?

Here's a couple things that might help:

Pass the click event along with the id into the backend code like this:

<button mat-icon-button (click)="onClickDelete($event, row.id)">
      <mat-icon>delete</mat-icon>
</button>

Then you can catch it in the ts. On the icon, you can try stopPropagation like this:

onClickDelete(e, id) {
    e.stopPropagation();
   // do stuff with the id;
}

On the row, one option is to check the targets class list:

onClickDelete(e, id) {
   if (e.target.className.includes('mat-icon-button')) {
       return;
   }
   //Do stuff with id
}

I had similar issue, i wanted to show pop up on icon click from table. Here is my workaround:

html

<ng-container matColumnDef="delete">
    <mat-header-cell *matHeaderCellDef> Delete </mat-header-cell>
    <mat-cell *matCellDef="let element">
        <button mat-button (click)="showAlert(element)">
            <i class="material-icons">delete_forever</i>
        </button>
    </mat-cell>
</ng-container>

<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;" (click)="selectedRows(row)"></mat-row>
</mat-table>

ts

...

public isClicked = false;

...

showAlert(action) {
    this.isClicked = true;

    //this open popup
    swal({
        //popup part
    }).then(isConfirm => {
        this.isClicked = false;
    }, (dismiss) => {
        if (dismiss === 'cancel' || dismiss === 'close') {
            this.isClicked = false;
        } 
    });
}

and then just check if the icon is clicked

selectedRows(row){
    if(!this.isClicked){
        //do what ever you want icon is not clicked
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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