Monday, April 20, 2009

Expanding tree nodes in Flex

I want to have a tree with all items expanded. So here is a little function to do that

private function expandTree(_tree:Tree, _data:Object = null):void
{
if (_data == null)
_data = _tree.dataProvider
if (_data != null && _data.length != null)
{
for (var i:int = 0; i <>
{
_tree.expandItem(_data[i], true)
if (_data[i]["children"] != null)
expandTree(_tree, _data[i]["children"])
}
}
}

As you can see this function accepts 2 params: tree and items to be expanded

If second parameter is null, all tree nodes will be expanded.

Function goes and opens node and then recursively child nodes

 _data.length != null means that we have a list object(e.g: Array, ArrayCollection, XMLList)

_data[i]["children"] != null means that node i from given collection has children.

Hope that this is helpful. 

I use Flex Builder 3.