如果我选择复选框,我会有一个无限循环
我如何使用Angular 8 获得检查项目的总和
她是我的代码
在您的示例中,必须简化相当多的代码。在重构TableSelectionExample类之后,我得到了以下相关方法:
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.length;
return numSelected === numRows;
}
toggleSelectionAll(): void {
this.isAllSelected() ?
this.selection.clear() :
this.dataSource.forEach(row => this.selection.select(row));
this.computeTotal();
}
toggleSelection(row: element): void {
this.selection.toggle(row);
this.computeTotal();
}
computeTotal(): void {
this.totalCal = this.dataSource
.filter(e => this.selection.isSelected(e))
.map(e => e.prixElement)
.reduce((a, b) => a + b, 0);
}
因此,我也不得不更改模板。
请查看修订后的StackBlitz。