问题描述
我有一个嵌套的带有可展开行的MAT表。只有内部表有复选框,默认情况下,所有嵌套表中的所有复选框都必须选中。这是有效的堆叠闪电战
https://stackblitz.com/edit/angular-nested-mat-table-triplenested-iavs62?file=app%2Ftable-expandable-rows-example.ts
getProjectDetails(element: any) {
console.log(element.Id);
this.tableService.getInnerData(element.Id).subscribe((res) => {
if (res.length == 0) {
element['usersdataSource'] = new MatTableDataSource();
} else {
element['usersdataSource'] = new MatTableDataSource(res);
this.userSelectionMap.set(
element.Id,
new SelectionModel<any>(true, [element['usersdataSource']])
); // provided datasource of the current inner table for default sselection
}
});
}
我在定义选择模型时提供了默认选择的数据源的值,但它没有按预期工作。我不想遍历每个表并进行选择,因为内表的数据量可能很大。实现此目标的最佳方式是什么?
推荐答案
实例化选择模型时,需要传递element['usersdataSource'].data
而不是[element['usersdataSource']]
。
getProjectDetails(element: any) {
console.log(element.Id);
this.tableService.getInnerData(element.Id).subscribe((res) => {
if (res.length == 0) {
element['usersdataSource'] = new MatTableDataSource();
} else {
element['usersdataSource'] = new MatTableDataSource(res);
this.userSelectionMap.set(
element.Id,
new SelectionModel<any>(true, element['usersdataSource'].data) // here
); // init userSelection of the current inner table
}
});
}