* Внесены некоторые изменения в HTML *
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle($event) : null" [checked]="selection.hasValue() && isAllSelected() == true" [indeterminate]="selection.hasValue() && !isAllSelected()">
</mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()" (change)="$event ? selectRow($event, row) : null" (change)="$event ? selection.toggle(row) : null" [checked]="selection.isSelected(row) == true">
</mat-checkbox>
</td>
</ng-container>
и добавлены некоторые функции в файл .ts
isAllSelected($event) {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}
masterToggle($event) {
if ($event.checked) {
this.onCompleteRow(this.dataSource);
}
this.isAllSelected($event) ?
this.selection.clear() :
this.dataSource.data.forEach(row => this.selection.select(row));
}
private selectRow($event, dataSource) {
// console.log($event.checked);
if ($event.checked) {
console.log(dataSource.name);
}
}
onCompleteRow(dataSource) {
dataSource.data.forEach(element => {
console.log(element.name);
});
}