【发布时间】:2020-07-13 14:46:21
【问题描述】:
我已在我的材料表中添加了复选框。除了我手动选择所有子复选框之外,一切都工作正常,标题复选框未选中。
HTML 代码:
<ng-container matColumnDef="select">
<mat-header-cell *matHeaderCellDef>
<label class="Ai-checkbox">
<input class="inputCheckBox" type="checkbox" name="checkBox" value="all" (change)="$event ? masterToggle() : null" [checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()">
</label>
</mat-header-cell>
<mat-cell *matCellDef="let row">
<label class="Ai-checkbox">
<input class="inputCheckBox" type="checkbox" name="checkBox" value="all" (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null" [checked]="selection.isSelected(row)">
</label>
</mat-cell>
</ng-container>
TS 文件:
/** Whether the number of selected elements matches the total number of rows. */
isAllSelected(){
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));
}
【问题讨论】:
标签: angular checkbox angular-material