所检查项目的总和,并无限循环停止

问题描述 投票:-2回答:1

如果选择复选框,我将陷入无限循环

我如何使用Angular 8获取检查项的总和

her is my code

angular angular-material
1个回答
0
投票

在您的示例中,必须简化一些代码。重构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

© www.soinside.com 2019 - 2024. All rights reserved.