-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tree.treeControl.expandAll() has an error #12469
Comments
* Currently when a developer uses `NestedTreeControl` with `MatTreeNestedDataSource` and calls `treeControl.expandAll`, a runtime exception will be thrown because no data nodes are set. This issue isn't an issue if someone uses `MatTreeFlatDataSource` because the data source requires the control to be passed to the data source. * Similarly should the `MatTreeNestedDataSource` also accept a tree control as constructor parameter. The data source should then delegate the data to the tree control in order to properly set up the tree control (similar to the flat data source) * Also adds safety checks and throws a more user-friendly error if someone implements his own `DataSource` and does not set the data nodes on the tree control and calls `expandAll` or `getDescendants` Fixes angular#12469
This is not a very friendly exception. I've created a PR that should improve that. For now though, you need to somehow set the myDataObservable.subscribe(data => {
this.dataSource.data = data
this.treeControl.dataNodes = data;
}); |
* Currently when a developer uses `NestedTreeControl` with `MatTreeNestedDataSource` and calls `treeControl.expandAll`, a runtime exception will be thrown because no data nodes are set. This issue isn't an issue if someone uses `MatTreeFlatDataSource` because the data source requires the control to be passed to the data source. * Similarly should the `MatTreeNestedDataSource` also accept a tree control as constructor parameter. The data source should then delegate the data to the tree control in order to properly set up the tree control (similar to the flat data source) * Also adds safety checks and throws a more user-friendly error if someone implements his own `DataSource` and does not set the data nodes on the tree control and calls `expandAll` or `getDescendants` Fixes angular#12469
tree.treeControl.expandAll() is worked for me. but I don't understand how to expand particular node in mat tree? |
@bhuvanp619 Either you use the const referenceToNode = database.data
.find(d => d.filename === 'Downloads')
this.nestedTreeControl.expand(referenceToNode); Here is a sample stackblitz: https://stackblitz.com/edit/angular-ar8tk4 |
@devversion I am using FlatTreeControl. will your solution work on FlatTreeControl? |
@bhuvanp619 The |
@devversion yes you are right. |
FWIW - in 7.0.4, tree control expands appear to work correctly for flat trees - this is because the datanodes propery of the tree control gets set by the flat data source. Nested tree controls get error with reduce being called on undefined because nothing sets the nested tree controls datanodes property. |
I ended up here from a stackoverflow question regarding how to preselect nodes. Here's a reproduction of |
Ran into this trying to do Setting the data twice as @devversion mentioned above works but is kind of an odd and error-prone API, in mo opinion. I'd expect the data source and the tree control to be more in sync rather than coordinating their data sources somewhat manually like that. The method shown above to expand a single method shown above ( Perhaps the API needs some better tooling around programmatically expanding nodes? The UI part of this story is much smoother. |
After spending more time with trees and data tables, I think more work needs to be done on the level of abstraction. The datasource connect method needs to be informed by various controls (tree control in nested and flat trees and paging and sorting controls in the table) that may change the data needs of the tree/table. So the datasource needs to have access to these, which most naturally owned by the component in question. For one, I'm thinking of adding the datasource methods to the component under development and having it be the datasource... If I actually do that I'll post the results. |
i want to give this a little push here. I just run into this issue and noticed, this is open for 3 years. but the workaround mentioned by devversion works for now. |
I have a nested tree structure with a single root node. Might the fix to this be an internal check on expandAll such that if it detects nested content it loops through the top level nodes and calls expand then expandDescendants on each?
|
Hi, Having the same problem for a Finally, the solution is so simple, we all never get it 😎 Just use the So this is :
|
I am using CDK 17.2.1 and 11 major versions later this problem is still present. I copied the nested tree example from the docs and then tried to call this.treeControl.dataNodes = MY_DATA_NODES;
this.treeControl.expandAll();
changeDetectorRef.markForCheck(); |
Bug
Dear team!
I has an error when I want to expand all node of Mat nested tree
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular 6.1.0
Material 6.4.1
The text was updated successfully, but these errors were encountered: