【发布时间】:2022-06-15 02:37:21
【问题描述】:
我有一个 Angular Material table 和一个 pagination 和一个 selection model 来获取一些行来做事。
目前我正在用server-side pagination 更改pagination,但是这样我的选择模型就坏了。当我更改页面时,我可以向选择模型添加行,并且工作正常。但是当我返回上一页时,行(在选择模型中)没有显示为选中状态。
我的选择模型,方法和angular material的demo page中发布的方法一样:
selection = new SelectionModel<ItemManagementViewDTO>(true, []);
isAllSelected(): boolean {
const numSelected = this.selection.selected?.length;
const numRows = this.items?.length;
return numSelected === numRows;
}
masterToggle(): void {
this.isAllSelected() ? this.selection.clear() : this.items?.forEach(row => this.selection.select(row));
}
checkboxLabel(row?: ItemManagementViewDTO): string {
if (!row) {
return `${this.isAllSelected() ? 'select' : 'deselect'} all`;
}
return `${this.selection.isSelected(row) ? 'deselect' : 'select'} row ${row.idItem + 1}`;
}
html 很简单:
<table mat-table [dataSource]="items" class="mat-elevation-z8" matSort>
<!-- Checkbox Column -->
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox
(change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()"
[aria-label]="checkboxLabel()"
color="primary"
>
</mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()" (change)="$event ? selection.toggle(row) : null" [checked]="selection.isSelected(row)" [aria-label]="checkboxLabel(row)" color="primary">
</mat-checkbox>
</td>
</ng-container>
<!-- ... -->
<mat-paginator [length]="totalItems" (page)="onPageChange($event)" [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
我的OnPageChange 和server-side 的分页是:
onPageChange(event: PageEvent): void {
this.itemsServ.getPaginatedItems(this.paramsStored, event.pageIndex, event.pageSize).subscribe(r => {
this.items = r.listDTO;
this.totalItems = r.totalElements;
}
我错过了什么?每次更改页面时,都会刷新源,但该项目仍然存在于选择模型中(如果我添加了它)。如果它与上一次调用中的项目相同,为什么它不会显示为选中?
提前致谢!
【问题讨论】:
标签: angular angular-material pagination