简体   繁体   English

使用Angular Material表时,如何为忽略一行的表行提供click事件处理程序

[英]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. 我的Angular Material表中有一行要单击的行。 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? 如何做到这一点,以便当我单击delete图标时,它不会调用整行的单击处理程序?

Here's a couple things that might help: 以下几件事可能会有所帮助:

Pass the click event along with the id into the backend code like this: 将click事件以及ID传递到后端代码中,如下所示:

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

Then you can catch it in the ts. 然后,您可以在ts中捕获它。 On the icon, you can try stopPropagation like this: 在图标上,您可以像这样尝试stopPropagation:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将表格的选定行值传递给按钮单击事件-材质设计-Angular 6 - How to pass selected row value of table to button click event - Material design - Angular 6 使用Angular如何停止表格行内复选框单击事件的传播()? - with Angular how to stopPropagation() of checkbox click event inside a table row? 图像映射中的单击事件后如何选择表格的一行? - How to select one row of table after click event in Image map? 使用jQuery,如何让click事件处理程序响应所选的表列? - Using jQuery, how to have click event handler respond for selected table columns? 如何获取物料表Angular中的行索引 - How to get row index in material table Angular 如何从 Angular 材料表中删除一行 - How to remove a row from an Angular Material table 如何为 Angular Material 表中的每一行创建 click () 事件的 observables 并响应所有这些事件? - How to create observables of click () events for every row in an Angular Material table and respond to all of them? 材料表:如何获取按钮点击时的行数据? (不在行选择上) - Material Table: How to get the row data on Button click? (Not on row selection) 如何获取角度的行单击事件? - How to get row click event in angular? 如何在mdtable有角材质2中单击行时添加左边框 - How to add left border on click of row in mdtable angular material2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM