Collapse Tree View Items via Scripting?

Is this possible? I have a recipe system that is using the tree view for organization, anytime I load a new recipe I would like to collapse the tree view so it’s obvious something has changed. Granted I’ll probably show a popup as well, but resting the objects on the screen to a “default” state would be ideal.

Had the same question a while back…

As Paul and I were just chatting, the Tree View now has a collapseAll() function. Wasn’t there when I asked back in 2010… :laughing:

tree = event.source.parent.getComponent('Tree View') tree.collapseAll()

I’m looking to collapse to a specific level ? is it possible ?
(Auto expand to level 1 is set on the tree and I want a buuton to collapse the tree back to this level)

Did you ever find a solution to this?

No sorry

Thanks, in my testing it appears the auto expand and auto expansion level properties are only considered when the tree first initializes, changing these properties when it’s open does not appear to have any effect. But if I place the tree view on a popup window, change these properties on an open window, close it then open it again the changes take effect.

Seems like the component just needs to be re-initialized after the property changes.

This should work:

tree = {referenceToTree}
tree.expandNodeForLevel(tree.model.root, {levelToExpand})
1 Like

Thanks! That’s what I needed. I did find some inconsistencies however:

In my use case, I have a search text field, when text is entered the goal is to expand the tree to show the details of the search results (filtering a list of products)… If the search text field is cleared then the tree should return to the default level expand of 1.

When searching, the tree level to expand is set to 2, when search is cleared level to expand is set to 1 using your code.

I found that it didn’t always return back to level 1 expansion after a I cleared my search. Expanding to level 2 works as expected but going back to 1 worked most of the time, but not all of the time.

Quick fix was to modify your code to collapse the tree before setting the level to expand, this produces a consistent result.

tree = {referenceToTree}
tree.expandNodeForLevel(tree.model.root, {levelToExpand})
1 Like