单击"Angular Material"表的行(单击)事件时,单击该行中的单元格中的动作 [英] Angular Material table row (click) event triggered when clicking an action in a cell within that row

查看:73
本文介绍了单击"Angular Material"表的行(单击)事件时,单击该行中的单元格中的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在mat-table中使用按钮触发模态而不触发行(click)事件?我看过并阅读

How can you trigger a modal with a button inside a mat-table without triggering the rows (click) event? I've seen and read with Angular Material 2 Table Mat Row Click event also called with button click in Mat Cell however the solution of using $event.stopPropagation() there prevents the modal from being displayed.

我遵循了这个 https://stackblitz.com/edit/angular-material2-expandable-rows-filter-pagination-sorting?file=app%2Ftable-example.html 用于行扩展功能.

I followed this https://stackblitz.com/edit/angular-material2-expandable-rows-filter-pagination-sorting?file=app%2Ftable-example.html for the row expansion functionality.

这是我的代码的一小段:

Here's a small snippet of my code:

<mat-table [dataSource]="dataSource"  matSort>
        <ng-container matColumnDef="name">
          <mat-header-cell *matHeaderCellDef  mat-sort-header>Name</mat-header-cell>
          <mat-cell *matCellDef="let element">{{element.name}}</mat-cell>
        </ng-container>
        <ng-container matColumnDef="username">
          <mat-header-cell *matHeaderCellDef  mat-sort-header>Username</mat-header-cell>
          <mat-cell *matCellDef="let element">{{element.username}}</mat-cell>
        </ng-container>
        <ng-container matColumnDef="email" class="hideOnResponse">
          <mat-header-cell *matHeaderCellDef mat-sort-header>Email</mat-header-cell>
          <mat-cell *matCellDef="let element">{{element.email}}</mat-cell>
        </ng-container>
        <ng-container matColumnDef="action">
          <mat-header-cell *matHeaderCellDef></mat-header-cell>
          <mat-cell *matCellDef="let element">
              <a (click)="editItem(element)" data-toggle="modal" data-target="#editor" matTooltip="Edit" aria-label="tooltip">
                <i class="fas fa-pencil-alt"></i>
              </a>
              <a (click)="deleteItem(element.id)" data-toggle="modal" data-target="#editor" matTooltip="Delete" aria-label="tooltip">
                <i class="fas fa-trash-alt"></i>
              </a>
          </mat-cell>
        </ng-container>

        <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
        <mat-row *matRowDef="let row; columns: displayedColumns;" class="element-row" [ccDetailRow]="row" [ccDetailRowTpl]="tpl"></mat-row>
        <mat-footer-row *matFooterRowDef="['loading']" [ngClass]="{'hide':dataSource!=null}"></mat-footer-row>
        <mat-footer-row *matFooterRowDef="['noData']" [ngClass]="{'hide':!(dataSource!=null && dataSource.data.length==0)}"></mat-footer-row>
      </mat-table>
      <mat-paginator [pageSizeOptions]="[5, 10, 25, 100]" [pageSize]="5" showFirstLastButtons></mat-paginator>

我尝试做上面链接的问题上所述的事情,但无济于事.还有其他办法吗?

I've tried doing what was stated on the question linked above but to no avail. Is there any other way?

推荐答案

从这里获得答案. 链接

如威尔所说:

尝试将$ event.stopPropagation()添加到更深的点击之一 处理程序(例如在单元格上).

Try adding $event.stopPropagation() to one of the deeper click handlers (like on the cell).

尝试执行类似的操作:

<mat-cell *matCellDef="let group" (click)="$event.stopPropagation()">
    <button mat-button (click)="onDelete(group.id)">
        <mat-icon>delete</mat-icon>
    </button>
</mat-cell>

以您的情况为准,如果正确,则为:

      <mat-cell *matCellDef="let element" (click)="$event.stopPropagation()">
          <a (click)="editItem(element)" data-toggle="modal" data-target="#editor" matTooltip="Edit" aria-label="tooltip">
            <i class="fas fa-pencil-alt"></i>
          </a>
          <a (click)="deleteItem(element.id)" data-toggle="modal" data-target="#editor" matTooltip="Delete" aria-label="tooltip">
            <i class="fas fa-trash-alt"></i>
          </a>
      </mat-cell>

我以前拥有的是在扩展视图中具有编辑和删除"按钮.我将它们放在普通视图中,并添加了stopPropagation.它为我工作.我不确定为什么您的模态没有打开.

What I had before was that I had edit and delete button in expanded view. I put them in the normal view and added stopPropagation. It worked for me. I am not sure why your modal isn't being open.

这是我的项目中的代码:

here's code from my project:

<ng-container matColumnDef="recordDate">
  <th mat-header-cell *matHeaderCellDef mat-sort-header> Record Date </th>
  <td mat-cell *matCellDef="let request" (click)=$event.stopPropagation()> {{request.recDate | date }} 
  <button mat-icon-button>
    <mat-icon aria-label="Example icon-button with a heart icon" (click)="openDialog(true, request.requestId)">edit</mat-icon>
  </button>
  <button mat-icon-button>
    <mat-icon aria-label="Example icon-button with a heart icon" (click)="deleteRequest(request.requestId)">delete</mat-icon>
  </button>
</td>
</ng-container>

这篇关于单击"Angular Material"表的行(单击)事件时,单击该行中的单元格中的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆