如何在不触发行(单击)事件的情况下在mat-table中使用按钮触发模态?我已经看过Angular Material 2 Table Mat Row Click event also called with button click in Mat Cell并已阅读,但是在那里使用$ event.stopPropagation()的解决方案阻止显示模式。
这是我的代码的一小段:
<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>
我尝试做上面链接的问题上所述的事情,但无济于事。还有其他办法吗?
答案 0 :(得分:2)
从这里得到答案。 link
如威尔所说:
尝试将$ event.stopPropagation()添加到更深的点击之一 处理程序(例如在单元格上)。
尝试执行类似的操作:
<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。它为我工作。我不确定为什么您的模态没有打开。
这是我项目中的代码:
<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>