【发布时间】:2017-08-17 04:43:42
【问题描述】:
我有一个表,每行包含一个复选框。在表格标题中,我有一个Check All 复选框,用于切换所有表格行框。
我正在尝试实现一些逻辑,如果复选框的数量将超过特定限制,则显示错误并且不要切换表格行复选框或 checkall 框本身。
有一个问题是允许选中 checkAll 框,即使我返回 false。我的绑定有问题?
HTML:
<input type="checkbox" name="checkAll" [(ngModel)]="checkedAll" (ngModelChange)="toggleAllEmployees($event)">
组件:
toggleAllEmployees(flag) {
const temp = [];
if (flag) {
// If selecting all of these passes the max, throw an error
if (this.importResults.length > this.maxSelection) {
/* This is where I get to when I display the error message */
alert('The number of employees you are trying to select (' + this.importResults.length + ') exceeds the max limit.');
return false;
} else {
for (let i = 0; i < this.importResults.length; i++) {
if (this.importResults[i].OnTempAssignment !== 'True') {
this.importResults[i].checked = true;
temp.push(this.importResults[i]);
}
}
this.employeeSelection = temp;
// Emit our changes
this.selectedEmployees.emit(this.employeeSelection);
}
} else {
//The else statement just un-checks all the boxes.
}
}
虽然没有选中任何表格行复选框,并且正确显示了错误,但我单击的框仍然被切换。
我认为return false; 会阻止这种情况,但我想不会。我的绑定是否有问题导致它无论如何都可以切换?
【问题讨论】:
-
你试过了吗
event.preventDefault(); -
@AravindI 做了,但也没有用。
$event只是发送true/false布尔值,而不是实际的事件对象。 -
你也可以使用pointer events css
标签: javascript angular typescript angular2-ngmodel