2 Replies Latest reply on May 15, 2009 12:45 AM by seiza13

    making tree using recursion

    seiza13

      ll!

       

      well, i have some troubles when i copy folders recursively between trees.

      for example if i click on a folder of my tree_1 i want to copy this folder and its fathers

       

      Tree1

      >A

        >B

          >C

           - C.1

           - C.2

       

      when i click on folder "C"

       

      Tree2

      >A2

      >A

        >B

          >C

            -C.1

            -C.2

       

      can anyone show me some example?

       

      Thanks

        • 1. Re: making tree using recursion
          _Natasha_ Level 4

          Hi,

           

          as a very quick example using XML as a dataProvider.

          Note, that you should use XMLListCollection or ArrayCollection to store your data to show changing data. If you use other arrays you should manually change tree.dataProvider = tree.dataProvider to show changes, but all nodes closed so it will be unable to open the tree.

           

          Hope it's helpful.

           

          <?xml version="1.0" encoding="utf-8"?>

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

          <mx:states>

          <mx:State name="alerting">

          <mx:AddChild relativeTo="{newView}" position="before">

          <mx:Label text="hehe"/>

          </mx:AddChild>

          </mx:State>

          </mx:states>

          <mx:Script>

          <![CDATA[

          import mx.utils.ObjectUtil;

          import mx.events.TreeEvent;

          import mx.controls.Alert;

           

          private var n:int = 1;

          private function onItemOpen(event:TreeEvent):void

          {

          var newItem:XML;

           

          var itemData:XML = event.item as XML;

          newItem = ObjectUtil.copy(itemData) as XML;

          newItem.@label = newItem.@label + " " + n.toString();

          delete newItem.*; // delete all childs

          var parent:XML = itemData.parent();

          while (parent.childIndex() != -1) // root has -1

          {

          var addNode:XML = ObjectUtil.copy(parent) as XML;

          delete addNode.*;

          addNode.@label = addNode.@label + " " + n.toString(); // add id

          addNode.appendChild(newItem);

          newItem = addNode;

          parent = parent.parent();

          }

          //Alert.show(newItem.toXMLString());

          n++;

          tstData.source[0].appendChild(newItem);

          tstData.refresh();

          }

          ]]>

          </mx:Script>

           

          <mx:XMLListCollection id="tstData">

          <mx:source>

          <mx:XMLList xmlns="">

          <root>

          <item label="Level1">

          <item label="Level2">

          <item label="Level3">

          <item label="Level4">

          <item label="level5"/>

          </item>

          </item>

          </item>

          </item>

          </root>

          </mx:XMLList>

          </mx:source>

          </mx:XMLListCollection>

          <mx:HBox x="0" y="0" width="100%" height="100%">

          <mx:Tree id="oldView" width="50%" height="100%" showRoot="false" dataProvider="{tstData}" labelField="@label" itemOpen="onItemOpen(event)">

          </mx:Tree>

          </mx:HBox>

          </mx:Application>

          1 person found this helpful
          • 2. Re: making tree using recursion
            seiza13 Level 1

            Hi Natacha!

            your example helped me very much

            Thank you a lot!