Macromedia Flex Macromedia Flex
Expand the root node of a Tree
  Home

Dec 08, 2006 - Expand the root node of a Tree
Uses callLater and xml

We often want to automatically expand the first node of a tree as soon as it is displayed.  In this simple example, in an initApp function, we assign the dataProvider to the tree.  There is a period of time after this before the tree has rendered and will allow us to call expandItem on it, so we use callLater to delay the function call.

Tracy Spratt

<?xml version="1.0" encoding="utf-8"?>
<!-- Tree control example. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" height="100%"
    creationComplete="initApp()">

<mx:Script><![CDATA[
  /** run by creationComplete event */
  private function initApp():void
  {
    myTree.dataProvider = xmlTreeData;  //set the tree dataProvider
    callLater(expandFirstNode);        //since we just set the dataProvider, the tree is not ready yet
  }//initApp

  /**called by callLater to ensure that the tree is ready to have expandItem called on it*/
  private function expandFirstNode():void
  {
    myTree.expandItem(xmlTreeData,true);  //expand the node
  }
    
]]></mx:Script>

  <mx:Tree id="myTree" width="50%" height="100%" labelField="@label" />
 
  <mx:XML id="xmlTreeData">
    <node label="root">
      <node label="Mail Box">
          <node label="Inbox">
              <node label="Marketing"/>
              <node label="Personal"/>
          </node>
          <node label="Outbox">
              <node label="Professional"/>
          </node>
          <node label="Spam"/>
          <node label="Sent"/>
      </node>
      <node label="Saved">
          <node label="Work">
              <node label="Hardware"/>
              <node label="Software"/>
          </node> 
          <node label="Personal">
            <node label="Home"/>
            <node label="Shopping">
                <node label="Christmas"/>
                <node label="Birthday"/>
            </node>
          </node>                  
      </node>  
    </node>
  </mx:XML>

</mx:Application>

File Details
Created On Dec, 08, 2006 by Tracy Spratt
Last Modified On Dec, 08, 2006 by Tracy Spratt
Group: Tips and Articles
Flex Versions: 2.0
Category: Controls:Lists (eg DataGrid/Tile/Tree)
Type: Complete Lesson
Difficulty: Beginner
Keywords:
Comments (2)
March 12, 2008 11:17PM - Vimal Bhatt
Just FYI - Somehow myTree.expandItem(xmlTreeData,true); not worked. So I used myTree.expandItem(myTree.firstVisibleItem,true) and it worked.

March 12, 2008 11:18PM - Vimal Bhatt
and I am using Flex 3, just in case things might have been changed.

You must be logged in to post.