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

    Expanding a Tree Node

    em3m
      Hello,

      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()">
      <mx:Script>
      <![CDATA[

      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
      callLater(expandSelectedNode);
      }
      }

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

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

      ]]>
      </mx:Script>

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

      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
          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
              paranoid945
              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()">
              <mx:Script>
              <![CDATA[
              import mx.controls.Alert;

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

              /**
              * 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()) {
              recursive(element);
              }
              }

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

              private function handleItemOpen(event:Event):void {
              event.preventDefault();
              return;
              }
              ]]>
              </mx:Script>
              <mx:HTTPService id="treeHttp" url=" http://127.0.0.1:8080/test.xml" 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:XMLListCollection>
              </mx:Tree>
              <mx:TextArea x="349" y="59"/>
              <mx:Button x="349" y="111" label="send http ->" click="treeHttp.send()"/>

              </mx:Application>
              • 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