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

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

问题描述

如何在不触发行(单击)事件的情况下触发带有垫子表内按钮的模态?我已经看到并阅读了 Angular Material 2 Table Mat Row Click 事件也通过 Mat Cell 中的按钮单击调用 但是使用 $event.stopPropagation() 的解决方案会阻止显示模态.

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.

这是我的一小段代码:

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

这是我项目中的代码:

<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天全站免登陆