首页 > 解决方案 > 全选复选框不应选择禁用的复选框记录角度

问题描述

有数据表,如果选中所有复选框,需要选择哪些在启用的复选框记录中并避免禁用记录。我在下面的代码中尝试了这个,我无法限制禁用的记录选择。

  checkuncheckall() {
    const totalChecked = this.persons.filter(f => f.checked).length;
    const target = totalChecked !== this.persons.length;
    this.persons.forEach(f => (f.checked = target));
    this.cdRef.detectChanges();
  }

html代码:

<td><input type="checkbox" [disabled]="person.firstName === 'Superman'" class="checkboxCls" [value]="person.checked" [checked]="person.checked" name="id" (change)="person.checked = !person.checked"></td>

演示

标签: angulartypescript

解决方案


checkuncheckall() {
    //First filter enabled checkbox
    const filteredCheckboxes = this.persons.filter(f => f.firstName !== "Superman");
    const totalChecked = filteredCheckboxes.filter(f => f.checked).length;
    const target = totalChecked !== filteredCheckboxes.length;
    filteredCheckboxesforEach(f => f.checked = target);
    this.cdRef.detectChanges();
  }

推荐阅读