6 Replies Latest reply on Oct 16, 2007 2:39 AM by em3m

    Expanding a Tree Node


      I have a tree that fetches the children of a branch on a mouse click on that specific branch. However, after fetching the child items, I would like to have that tree item expand to show these items. I have searched the net for a solution and the expandItem doesn't quite work as expected. I don't know if this is the right way. Here are some parts of my code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">

      public function onTreeChange(event:Event):void {
      if(productTree.selectedItem) {
      // Obtain the attributes with which the child items are fetched.
      // Call the function that fetches the child items from the database which returns an XML
      .... some other processing

      private function expandSelectedNode():void {
      productTree.expandItem(productTree.selectedItem, true);

      private function initApp():void {
      // Obtain the root elements to be shown in the tree


      <mx:Tree dataProvider="{nodes}" id="productTree" change="onTreeChange(event)"></mx:Tree>

      Can someone please suggest a possible solution to obtain such functionality or tell me where I would be going wrong.

      Thank you.
        • 1. Re: Expanding a Tree Node
          Flextron Level 1
          It may look stupid, but works. Try this.
          private function expandSelectedNode():void {
          productTree.expandItem(productTree.selectedItem, false);
          productTree.expandItem(productTree.selectedItem, true);
          • 2. Re: Expanding a Tree Node
            em3m Level 1
            thanks flextron for your reply.. I tried the solution you provided but it still doesn't expand the node after the child items have been appended.. did you face a similar problem?
            • 3. Re: Expanding a Tree Node
              are these codes work for you?

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="treeHttp.send()">
              import mx.controls.Alert;

              private function handleResult():void {
              treeData.source = XMLList(treeHttp.lastResult);
              var root:XML = XML(treeData.getItemAt(0));

              * recursively opens proper branch.
              private function recursive(node:XML):void {
              var isBranch:String = node.@isBranch;
              var isOpen:String = node.@isOpen;
              if (("true" == isBranch) && ("true" == isOpen)) {
              navTree.expandItem(node, true);
              for each (var element:XML in node.elements()) {

              private function handleFault():void {
              Alert.show("http error!");

              private function handleItemOpen(event:Event):void {
              <mx:HTTPService id="treeHttp" url="" resultFormat="xml" result="handleResult()" fault="handleFault()" />
              <mx:Tree id="navTree" x="37" y="59" width="300" height="600" labelField="@label"
              dragEnabled="true" dropEnabled="true" itemOpening="handleItemOpen(event)"
              <mx:XMLListCollection id="treeData">

              <mx:TextArea x="349" y="59"/>
              <mx:Button x="349" y="111" label="send http ->" click="treeHttp.send()"/>

              • 4. Re: Expanding a Tree Node
                em3m Level 1
                thanks for the code.. i tried it but my requirement is slightly different and it didn't work as I wanted it to... Thanks once again.. i don't know if anyone else has faced such a situation.
                • 5. Re: Expanding a Tree Node
                  mae@ccg Level 1
                  I wonder if you are expanding an empty node. I know you've got the callLater(), but you might try expanding the node in the result of your fetch.
                  • 6. Re: Expanding a Tree Node
                    em3m Level 1
                    thanks alot mae.. it worked when I called the expandItem() right after fetching the child items. thank you all for your help