【发布时间】:2020-10-23 21:13:19
【问题描述】:
我整天都在尝试让mat-selection-list 在刷新数据时保留选定的对象,即。分页但没有成功。
如果我在第 1 页中选择对象 A ({name, id}),然后切换到第 2 页并返回第 1 页,我希望对象 A 仍被标记为已选中,但这不会发生,我输了我的想法。
我已经尝试了几件事:
- 2路绑定
[(ngModel)]="selectedOptions" [compareWith]="compareFunction"。但是当我切换到第 2 页时,由于 2 路绑定,mat-selection-list 会自动清除selectedOptions,因为在我切换到第 2 页时它无法在列表中找到对象。
- 使用带有
[formControl]="itemsControl" (selectionChange)="onSelectionChanged($event)"和form.setValue``. This way themat-selection-listdoesn' clear it but when returning to page 1 objects are no rechecked automatically. If I select one thenonSelectionChange 的响应式表单将当前表单报告为唯一选定的对象。
我的想法已经用完了,用谷歌搜索了几个小时,我开始认为这是一个错误?
非常感谢任何帮助。
【问题讨论】:
-
在将项目插入现有列表时,我看到了非常相似的行为。即使通过
[selected]="item.selected"设置选定项目状态,即使{{ item.selected }}显然仍然为真,它也会被取消选中。