# Collapse Tree View Items via Scripting?

**URL:** <https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226>\
**Category:** Ignition\
**Created:** [October 24, 2014, 7:37pm UTC](https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226 "2014-10-24T19:37:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paullys50](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paullys50/32/3324_2.png) [@Paullys50](https://forum.inductiveautomation.com/u/Paullys50)\
**Post date:** [October 24, 2014, 7:37pm UTC](https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226/1 "2014-10-24T19:37:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [October 24, 2014, 7:54pm UTC](https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226/2 "2014-10-24T19:54:58Z")

</div>

Had the same question a while back…

[https://www.inductiveautomation.com/forum/viewtopic.php?f=70&t=5199&p=12502&hilit=tree+collapse#p12502](https://www.inductiveautomation.com/forum/viewtopic.php?f=70&t=5199&p=12502&hilit=tree+collapse#p12502)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [October 24, 2014, 8:42pm UTC](https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226/3 "2014-10-24T20:42:31Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [November 23, 2018, 8:49am UTC](https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226/4 "2018-11-23T08:49:03Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Paullys50](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paullys50/32/3324_2.png) [@Paullys50](https://forum.inductiveautomation.com/u/Paullys50)\
**Post date:** [March 11, 2020, 1:53pm UTC](https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226/5 "2020-03-11T13:53:36Z")

</div>

> [@mazeyrat](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [March 11, 2020, 1:54pm UTC](https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226/6 "2020-03-11T13:54:37Z")

</div>

No sorry

---

<div class="post-metadata">

**Author:** ![Paullys50](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paullys50/32/3324_2.png) [@Paullys50](https://forum.inductiveautomation.com/u/Paullys50)\
**Post date:** [March 11, 2020, 2:27pm UTC](https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226/7 "2020-03-11T14:27:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 11, 2020, 4:09pm UTC](https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226/8 "2020-03-11T16:09:47Z")

</div>

This should work:

```python
tree = {referenceToTree}
tree.expandNodeForLevel(tree.model.root, {levelToExpand})

```

---

<div class="post-metadata">

**Author:** ![Paullys50](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paullys50/32/3324_2.png) [@Paullys50](https://forum.inductiveautomation.com/u/Paullys50)\
**Post date:** [March 11, 2020, 5:19pm UTC](https://forum.inductiveautomation.com/t/collapse-tree-view-items-via-scripting/9226/9 "2020-03-11T17:19:20Z")

</div>

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.

```auto
tree = {referenceToTree}
tree.collapseAll()
tree.expandNodeForLevel(tree.model.root, {levelToExpand})

```
