У меня есть следующий код с таблицей материалов, которая выбирает несколько столбцов. Я заметил, что на каждое движение мыши isAllSelected()
вызывается сотни раз. Почему это происходит. Я думал, что это должно быть вызвано только когда флажок был установлен
<mat-table #table [dataSource]="dataSource">
<!-- Checkbox Column -->
<ng-container matColumnDef="checkbox">
<mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null" [checked]="selection.hasValue() && isAllSelected()">
</mat-checkbox>
</mat-header-cell>
<mat-cell *matCellDef="let priority">
<mat-checkbox (click)="$event.stopPropagation()" (change)="$event ? selection.toggle(priority) : null"
[checked]="selection.isSelected(priority)">
</mat-checkbox>
</mat-cell>
</ng-container>
<!-- Colour Column -->
<ng-container matColumnDef="colour">
<mat-header-cell *matHeaderCellDef>Colour</mat-header-cell>
<mat-cell *matCellDef="let priority">{{priority?.colour}}</mat-cell>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
<mat-cell *matCellDef="let priority">{{priority?.name}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let priority; columns: displayedColumns;" class="contact" (click)="selection.toggle(row)">
</mat-row>
</mat-table>
и файл .ts
export class AdminPrioritiesSettingsComponent {
dataSource: MatTableDataSource<any>;
selection = new SelectionModel<PriorityDto>(true, []);
displayedColumns = ['checkbox', 'colour', 'name'];
isAllSelected() {
console.log(this.selection.hasValue());
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}
/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
this.isAllSelected() ?
this.selection.clear() : this.dataSource.data.forEach(row => this.selection.select(row));
}
}