简体   繁体   English

如果在 Angular 2+ 中选中了三个以上,则禁用复选框

[英]Disabled checkboxes if more than three checked in Angular 2+

I've created a table in angular material and i will compare the data of three rows.我已经用角材料创建了一个表格,我将比较三行的数据。 Now i have to disable all other checkboxes if i have three boxes on checked.现在,如果我选中了三个复选框,则必须禁用所有其他复选框。 I know i could use the [disabled] state from the material table, but if i checked three boxes, all boxes are disabled than.我知道我可以使用材料表中的 [disabled] 状态,但是如果我选中三个框,则所有框都被禁用。 But i have to deselect the checked boxes so i could change the collection.但我必须取消选中复选框,以便我可以更改集合。

How could i write a if else statement?我怎么能写一个if else语句?

Working tree工作树

Just add !selection.isSelected(row) in the disabled state :只需在禁用状态添加 !selection.isSelected(row) :

 <!-- Checkbox Column -->
  <ng-container matColumnDef="select">
    <th mat-header-cell *matHeaderCellDef></th>
    <td mat-cell *matCellDef="let row">
      <mat-checkbox (click)="$event.stopPropagation()" (change)="$event ? selectRow($event, row) : null" (change)="$event ? selection.toggle(row) : null" [checked]="selection.isSelected(row) == true" [disabled]="disableBoxes && !selection.isSelected(row)"
      >
      </mat-checkbox>
    </td>
  </ng-container>

Working Demo工作演示

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM