如何以角度访问来自 2 个不同 mat-tree 的数据? [英] How to access data from 2 different mat-tree in angular?
问题描述
在我的项目中,我使用了来自角度材料的 2 种不同的树.
in my project, I am using 2 different trees from angular material.
我在这里创建了一个关于 stackblitz 的演示:https://stackblitz.com/edit/angular-wvbg5j.
I created a demo on stackblitz here : https://stackblitz.com/edit/angular-wvbg5j.
当我尝试访问他们的数据时,我使用 treeControl.dataNodes 但它只给我来自最新生成的树(groceries2 和提醒 2 树)的数据.每次我选中/取消选中一个项目时,它都会在控制台中显示 dataNodes.
When I try to access their data, I use treeControl.dataNodes but it only gives me the data from the latest generated tree ("groceries2 and reminders2 tree). It displays dataNodes in the console each time I check/uncheck an item.
所以我不知道该怎么办,还有其他方法可以访问树数据吗?或者是否有将两棵树都存储在内存中的函数?
So I don't know what to do, is there another way to access tree data? or is there a function that stores both trees in memory?
推荐答案
在处理复选框更改时,只需将数据源添加为参数即可:
Just add the data source as an argument when you handle the checkbox change :
(change)="todoLeafItemSelectionToggle(node, dataSourceX)"
在你的函数中只需访问数据属性:
And inside your function just access to the data attribute :
todoLeafItemSelectionToggle(node: TodoItemFlatNode, dataSource : MatTreeFlatDataSource<TodoItemNode, TodoItemFlatNode>): void {
console.log(dataSource.data) ;
this.checklistSelection.toggle(node);
this.checkAllParentsSelection(node);
this.getParentNode(node)
}
这篇关于如何以角度访问来自 2 个不同 mat-tree 的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!