Skip to content
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

docs-bug(treeview): Add guide on how to migrate from old treeview to new treeview #29919

Open
azimuthdeveloper opened this issue Oct 24, 2024 · 4 comments
Labels
area: cdk/tree docs This issue is related to documentation P4 A relatively minor issue that is not relevant to core functions

Comments

@azimuthdeveloper
Copy link

azimuthdeveloper commented Oct 24, 2024

Documentation Feedback

Following the sudden deprecation of the various tree controllers, it would be nice if there were some kind of documentation on how to remove tree controller and migrate to the new way of doing things. Also, the documentation still contains references to treeControl, which it shouldn't because the treeControl has been deprecated?

Image

A simple migration guide would be warranted as well because there are tonnes of search results and guides on how to use the tree that just became out of date, so if there was an easy way to adapt code to the new way of doing things, that would be ideal.

Affected documentation page

https://material.angular.io/components/tree/overview

@azimuthdeveloper azimuthdeveloper added docs This issue is related to documentation needs triage This issue needs to be triaged by the team labels Oct 24, 2024
@andrewseguin andrewseguin added P4 A relatively minor issue that is not relevant to core functions area: cdk/tree and removed needs triage This issue needs to be triaged by the team labels Oct 25, 2024
@azimuthdeveloper
Copy link
Author

azimuthdeveloper commented Oct 26, 2024

Actually, the tree documentation page needs some attention overall it seems.

In the v17 documentation, the "Tree with flat nodes" appears correct.

Image

But then, in v 18.2.10, it looks wrong.

Image

I think I'm reasonably good at the tree - I did an implementation for work and also wrote a fairly long article for LogRocket so I might be able to pop in a PR for these things, and fix other things (https://blog.logrocket.com/angular-tree-flat-vs-nested-and-more/).

Also, the various selectors seem to rely on knowing about the children beforehand and don't support returning asynchronously. When tree children are loaded from the server, how is this supposed to be handled?

Kind of not really related - I find the documentation for the treeview to be simultaneously overwhelming and light on details, almost like it's been built up over several years. This confusion is now only worse with the API documentation chopping and changing between the old treeControl implementation and the new childrenAccessor. People who were new to the tree would find the documentation very confusing in its current state. It's like the whole page needs to be redone. Would you accept a PR for this?

@azimuthdeveloper
Copy link
Author

Just to help other people who might be trying to use async data in their trees: I figured it out. I tried a lot of things and in the end this is what stuck. My branch is here, I'm going to write about it soon with samples etc: https://github.com/azimuthdeveloper/angulartree/tree/inflight-changes-to-new-angular-tree

Basically,

For flat trees, use levelAccessor, and then on your node, use expandedChange to fire an event like handleNodeExpansion($event, node) where $event is the true/false state of whether the node is expanded or not, and node is the node that is being actuated. In that function, call your expand/contract function in the data source.

For nested trees, use childAccessor, and assign it to the children property of your node. It'll look like this. children must be of type Observable, so when they update, the tree updates. In my case, its a BehaviourSubject

childrenAccessor = (dataNode: NestedTreeNode) => dataNode.children;

Again, then on your node, use expandedChange to fire an event like handleNodeExpansion($event, node) where $event is the true/false state of whether the node is expanded or not, and node is the node that is being actuated. In that function, call your expand/contract function in the data source.

On a more personal note, I think Angular has been going from strength to strength lately, and I wouldn't want to get on here and complain or say anything derogatory. Please accept this as constructive feedback.

At the time of me writing this, the tree control sample page is an unintelligible mess, chopping back and forth between the old treeControl and new childrenAccessor/levelAccessor. The documentation was very hard to understand in the first place and now it's just gotten worse. There are samples on how to use childrenAccessor, but no samples on how to use levelAccessor, so people are free to guess their way through an implementation like I did.

It's not clear why the tree was revamped - I couldn't find a long issue with comments that ended in "okay I'm going to deprecate large parts of the tree and make it work differently" so it's difficult to understand why this change was even made in the first place.

It would be nice if, when new features shipped, there was a better copy-edit process on the documentation, documentation didn't ship with todo notes, documentation was at least at feature parity with previous releases (like async loading of nodes), documentation at least covered all features (such as levelAccessor being absent) and some rationale was given as to why the change occured. I'm a strong Angular supporter, I write about it professionally and use it in my day job, so this comes from a place where I want Angular to succeed in the long run.

@Sebi11
Copy link

Sebi11 commented Jan 15, 2025

I almost totally agree with you @azimuthdeveloper about the lack of documentation. Just one point to be fair: the new code to use MatTree seems way more simple and shorter than the version before Angular Materiel 18.2.

@andrewseguin
Copy link
Contributor

To remark on why the tree changed, in short we had a number of accessibility issues with how the tree worked in the past. As we made changes to improve the accessibility, we had to make adjustments/improvements to the API. You can see a detailed note of the changes and their reasons in the original PR #29062

I agree the tree's documentation is subpar. Unfortunately we ran out of resources to give it the update it deserves. In particular I do wish we had a better guide on transitioning from the deprecated API to the current.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: cdk/tree docs This issue is related to documentation P4 A relatively minor issue that is not relevant to core functions
Projects
None yet
Development

No branches or pull requests

3 participants