简体   繁体   English

使用Angular如何停止表格行内复选框单击事件的传播()?

[英]with Angular how to stopPropagation() of checkbox click event inside a table row?

I have an angular material table with a row showing customers and another child table showing their orders.我有一个角度材料表,其中一行显示客户,另一个子表显示他们的订单。 On clicking anywhere in the row it expands the row for showing the customers orders.单击行中的任意位置时,它会展开行以显示客户订单。 When I click in the checkbox in the customer row I don't want the orders child table to open.当我单击客户行中的复选框时,我不希望打开订单子表。 I have tried a couple of things but with no luck.我尝试了几件事,但没有运气。 Can someone recommend what would work with this scenario.有人可以推荐什么适用于这种情况。 Your helps really appreciated, thanks!非常感谢您的帮助,谢谢!

I have tried putting (click)="$event.stopPropagation(); on the checkbox, on the container as well and that didnt work. I also tried to make a directive as shown in In Angular, how to stopPropagation() of TR click event when checkbox has change event? but this still does not stop the (click)="expandCollapse(row) on the <tr> from firing.我试过把(click)="$event.stopPropagation();放在复选框上,放在容器上,但没有奏效。我还尝试制定一个指令,如在 Angular, how to stopPropagation() of TR 中所示复选框具有更改事件时的单击事件?但这仍然不会阻止<tr>上的(click)="expandCollapse(row)触发。

Checkbox code:复选框代码:

<ng-container *ngIf="column.key == 'all' && column.key !== 'sort'">
  <th mat-header-cell *matHeaderCellDef [style.width.px]="75">
    <mat-checkbox (click)="$event.stopPropagation();
      (change)="$event ? masterToggle() : null"
      [checked]="hasValue() && isAllSelected()"
      [indeterminate]="hasValue() && !isAllSelected()"
    >
    </mat-checkbox>
    <span class="checkbox-text"></span>
  </th>
  <td mat-cell *matCellDef="let row">
    <mat-checkbox
      (change)="$event ? toggle(row.customerId) : null"
      [checked]="isSelected(row.customerId)">
    </mat-checkbox>
  </td>
</ng-container>

Parent material row:父材料行:

<tr mat-row
  [ngClass]="{'highlighted': isSelected(row.customerId) }"
  *matRowDef="let row; columns: columnKeys;" class="example-element-row"
  [class.example-expanded-row]="isSelected(row.customerId)"
  (click)="expandCollapse(row)" id="rowcustomer-{{row.rowIdex}}">
</tr>

If you followed the documentation sample of Angular Material ( Angular Material)如果您遵循 Angular Material ( Angular Material)的文档示例

by default you would have something like below in your component.html file:默认情况下,您的 component.html 文件中会有如下内容:

 <tr mat-row *matRowDef="let row; columns: displayedColumns;"
    (click)="selection.toggle(row)"
 </tr>

you remove the click event so it would look like below:您删除点击事件,使其如下所示:

<tr mat-row *matRowDef="let row; columns: displayedColumns;"
 </tr>

and it should work它应该工作

暂无
暂无

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

相关问题 在表格行中有一个输入复选框,我可以将click事件添加到行,仍然可以单击输入 - with an input checkbox inside a table row, can i add click event to row and still be able to click input 如何使用stopPropagation使toggleClass的click事件起作用? - How to make click event work for toggleClass with stopPropagation? 如何使整行以及其中的复选框都可以单击,然后在我在表行内部单击时单击并取消单击? - How to make entire row along with the checkbox in it, click and unclick when I anywhere click inside the table row? 第二次点击停止传播事件 - stoppropagation event on second click Mat-Menu上的StopPropagation,但某些单击事件除外-Angular - StopPropagation on Mat-Menu except certain click event - Angular 如何从复选框单击位于GridView行内的TextChanged事件背后的textBox代码? - How to call textBox codebehind TextChanged event from checkbox click lying inside GridView Row? 复选框click事件中的event.stopPropagation是否可以防止IE 8及更早版本中的change事件触发? - Does event.stopPropagation in a checkbox click event prevent the change event from firing in IE8 and earlier? 角度表,行中的复选框 - Angular table, checkbox in row 我如何将按钮单击事件与 vuetify 数据表中一行内的行单击事件分开 - how i can separate the buttons click event from row click event inside a row in a vuetify data-table JQuery:根据复选框单击更改表中行的颜色 - JQuery: Change the color of the row inside a table based on the checkbox click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM