【问题标题】:Angular Material table row (click) event triggered when clicking an action in a cell within that row单击该行中单元格中的操作时触发 Angular Material 表行(单击)事件
【发布时间】:2019-04-23 18:27:35
【问题描述】:

如何在不触发行(单击)事件的情况下触发带有垫表内按钮的模式?我已经看到并阅读了Angular Material 2 Table Mat Row Click event also called with button click in Mat Cell,但是在那里使用 $event.stopPropagation() 的解决方案会阻止显示模式。

我关注了https://stackblitz.com/edit/angular-material2-expandable-rows-filter-pagination-sorting?file=app%2Ftable-example.html 的行扩展功能。

这是我的代码的一个小sn-p:

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

我已尝试按照上面链接的问题所述进行操作,但无济于事。有没有其他办法?

【问题讨论】:

    标签: angular angular-material bootstrap-modal angular-cdk onrowclick


    【解决方案1】:

    从这里得到答案。 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>
    

    【讨论】:

    • 我已经尝试过了,但它会禁用模式显示。
    • 哦,我的错。在您的问题中没有注意到它。
    • 我是完全一样的可扩展表,我也有编辑和删除按钮。唯一的区别是我的按钮出现在行的扩展版本上。所以我不明白这个问题。这可能不是你想要的,但它可能是一个临时解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多