Angular全选复选框需跳过禁用项仅勾选启用项的实现咨询
复选框全选排除禁用项解决方案
核心修改逻辑
在遍历赋值选中状态时增加禁用状态判断,仅修改未禁用的复选框:
- 保留原有全选状态翻转逻辑
- 遍历条目时新增判断,跳过带禁用标记的条目
修改后代码
checkuncheckall() { this.isChecked = !this.isChecked; this.displaying_results.forEach(i => { // 仅对非禁用的复选框修改选中状态 if (!this.persons[i].disabled) { this.persons[i].checked = this.isChecked; } }); }
补充适配说明
如果你的项目中禁用状态对应的字段名不是disabled,请将上述判断条件中的字段名替换为你实际使用的字段即可,比如字段为isDisabled就调整为if (!this.persons[i].isDisabled)。
如果需要实现「手动把所有启用的复选框都勾选后,全选框自动变为选中状态」的联动效果,可以在单个复选框的选中事件回调中增加以下判断逻辑:
// 统计所有未禁用的复选框总数 const totalEnabled = this.persons.filter(item => !item.disabled).length // 统计已选中的未禁用复选框数量 const checkedEnabled = this.persons.filter(item => !item.disabled && item.checked).length // 自动同步全选框状态 this.isChecked = totalEnabled === checkedEnabled
内容的提问来源于stack exchange,提问作者Rajasekhar
相关产品推荐
相关产品推荐

