3

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?

2 Answers 2

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.