我正在尝试在实体平面树组件中实现搜索功能(过滤),但是更新我的dataSource时没有任何反应。我觉得我尝试了所有不同的方法,但无法正常工作。我要做的就是替换数据并重新渲染树。我在做什么错了?
我尝试了不同的解决方案,但其中许多似乎已过时,例如:
this.dataSource.data = null;
this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);
this.dataSource.data = [];
在尝试展平空对象时给出错误。
interface FlatNode {
expandable: boolean;
name: string;
level: number;
}
@Component({
selector: 'app-category-list',
templateUrl: './category-list.component.html',
styleUrls: ['./category-list.component.scss']
})
export class CategoryListComponent {
dataChange = new BehaviorSubject<Category[]>([]);
private _transformer = (node: Category, level: number) => {
return {
expandable: !!node.children && node.children.length > 0,
name: node.name,
level: level,
};
}
treeControl = new FlatTreeControl<FlatNode>(
node => node.level, node => node.expandable);
treeFlattener = new MatTreeFlattener(
this._transformer, node => node.level, node => node.expandable, node => node.children);
dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);
constructor(private categoryListService: CategoryListService) {this.dataSource.data = this.categoryListService.getTopCategories();}
hasChild = (_: number, node: FlatNode) => node.expandable;
ngOnInit() {
this.dataChange.subscribe(data => {
this.dataSource.data = data;
});
}
}
searchCategory(list: Category[]) {
this.dataChange.next(list);
}
}
我希望dataSource更新并重新渲染材料树。