在使用Angular CDK虚拟滚动的情况下,由于只有可见的元素会被渲染到DOM中,所以无法同时选择所有列中的mat-select。但是可以通过存储选择状态的数组来解决这个问题。以下是一个解决方法的代码示例:
selectedItems: any[] = [];
{{ item.label }}
toggleItemSelection(item: any) {
const index = this.selectedItems.indexOf(item);
if (index === -1) {
this.selectedItems.push(item);
} else {
this.selectedItems.splice(index, 1);
}
}
isSelected(item: any) {
return this.selectedItems.indexOf(item) !== -1;
}
这样,无论是可见的还是不可见的元素,都可以正确地保存和显示选择状态。