Opening nodes in a Flex Tree control using the expandItem() method

The following example shows you how you can expand tree nodes (recursively, no less) by selecting an item from a ComboBox control.

Full code after the jump.


<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<mx:Application xmlns:mx=""


            private function comboBox_change(evt:ListEvent):void {
                var team:String = ComboBox(evt.currentTarget).selectedItem.@label;
                var node:XMLList = == team);

                tree.selectedItem = node[0];
                var idx:int = tree.getItemIndex(node[0]);

            private function expandParents(node:XML):void {
                if (node && !tree.isItemOpen(node)) {
                    tree.expandItem(node, true);

    <mx:XML id="mlb" source="mlb.xml" />

    <mx:ApplicationControlBar dock="true">
        <mx:ComboBox id="comboBox"
                prompt="Please select a team..."
                change="comboBox_change(event);" />

    <mx:Tree id="tree"
            rowCount="8" />


View source is enabled in the following example.

8 thoughts on “Opening nodes in a Flex Tree control using the expandItem() method

  1. By the way, there is a bug in the previous example. For cities that have multiple teams (Los Angeles, Chicago, New York) the first matching city is always returned. Sorry, I should have given each team a more unique ID than just a city name.


  2. Cheers Buddy! I had been mucking around with expandItem() for a few hours and did not realize the tree will not “auto expand” you have to do it manually so thanks for that you just saved me another hour or so of mucking around (=

  3. Thanks for the example, but there’s another issue with this code that I’d like to bring up. If you go into the tree and expand nodes all the way down to the leaf, then close the “main” parent node and THEN try your code to expandParents it will not work.

    The reason is because subnodes have the property isOpen set to true because you only closed their “main” node, but not each of the subnodes. Does that make sense?

    The fix is very easy. Just need one more case in the recursion:

    else if(node){epandParents(node.parent();)}

    This will ensure that the recursion will reach all the way up to the root node and expand that if that’s the only one you closed.

    Besides that one little issue the code is awesome and thanks for the example!

Comments are closed.