Angular Material-在数据源更改时保留原始复选框的选择

时间:2018-12-18 09:14:28

标签: angular typescript angular-material

在mat-table中,当更改数据源然后又将其还原时,我希望再次检查最初选中的复选框。 假设我选择了复选框1和2。然后更改了数据源。然后将其还原为之前的状态。在这种情况下,我希望较早选择的复选框1和2被自动选中。选择中包含这些记录,但不会对其进行检查。

我有一个mat-table,其复选框列为:

<ng-container matColumnDef="selectColumn">
    <mat-header-cell *matHeaderCellDef></mat-header-cell>
    <mat-cell *matCellDef="let element">
    <input id="checkBox" type="checkbox" (change)="$event ? selection.toggle(element) : null; updateSelection();"
        [checked]="selection.isSelected(element)" (click)="$event.stopPropagation();">
    </mat-cell>
</ng-container>

TS:

selection = new SelectionModel(true, AppConstants.selectedRecords);

updateSelection() {
    console.log(this.selection.selected);
}

createRecordingsTable() {
    // ... logic to update this.records
    this.dataSource = new MatTableDataSource(this.records);
    console.log(this.selection.selected);
}

现在,当数据源恢复为原始数据时,即使选择中有条目也不会选中复选框1和2。另外,如果我再次单击1和2,将在选择中创建重复的条目。

我该如何解决?

0 个答案:

没有答案