Expanding Flex Tree Control / Tree in Flex 2.0

Friday, 10 August 2007, 21:58 | Category : Flex
Tags :

It’s been some time since i have been writing code for displaying trees for different modules in my project. A tree is a very useful component as it its fast to navigate, hierarchical, can be sorted, expands/collapses so requires less space and what not. Over this period of time i have come across some code snippets which may definitely prove to be useful for any Flex developer working with trees. Let me kick them one by one.

Expanding the complete tree from the root node and select a particular node – This is one of the common requirements and can be easily established by using the following snippet. Once you have done whatever you want to do with the tree just put a call to the function –

expandTree - is a function which will expand the root node
[newNode] - is an array of arguments which will be passed to the expandTree().
callLater(expandTree, [newNode]);

A sample expandTree function is –

private function expandTree(childNode : Object) : void {
treeID.expandChildrenOf(treeID.getChildAt(0), true);        
treeID.selectedItem = childNode;

The assignment of expandTree to callLater sets the data provider properly and then trigger a call for expanding the tree.