23rd Jan 2009

Expanding All Tree Nodes

I am working on a project that requires the data provider for a tree component to be reloaded periodically. When I would reload the data, the tree would revert to it’s collapsed state. From a usability point of view, this was not the best of situations. So I started doing some research how to expand all of the nodes in a tree. I came across of a couple of places using loops such as:

private function expandTree():void{
for (var i:int = 0; i < thisTree.dataProvider.length; i ++){
thisTree.expandChildrenOf(thisTree.dataProvider[i], true)
}
}
private function collapseTree():void{
for (var i:int = 0; i < thisTree.dataProvider.length; i ++){
thisTree.expandChildrenOf(thisTree.dataProvider[i], false)
}
}

When looking at this, I noticed the name of the function is expandChildrenOf, which to me inferred it would expand all of the children of the given node. So I looked into a bit further and the description for this function in the documentation states (http://livedocs.adobe.com/flex/2/langref/mx/controls/Tree.html#expandChildrenOf()):

expandChildrenOf () method
public function expandChildrenOf(item:Object, open:Boolean):void

Opens or closes all the tree items below the specified item.
Parameters
item:Object — the starting item

open:Boolean — toggles an open or close operation

So if that is the case and I pass this function the root node of the dataProvider such as in the following:

this.myTree.expandChildrenOf(this.myTree.dataProvider[0],true);

It did as expected and expanded all of the nodes under the root node. No loop required.

One Response to “Expanding All Tree Nodes”

  1. MCruz Says:

    Awesome !!!