0 Replies Latest reply on Aug 22, 2009 5:25 AM by VinodhaPalanisamy

    How to add child node and child items into a parent node in tree in Flex?

    VinodhaPalanisamy

      Hi all ,

                 This is the code to add child node and child items into a parent node in tree in flex. With out use CustomClass for descriptor in Tree.

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

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

      <mx:Application

       

      xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"

      >

      <mx:Script>

      <![CDATA[

       

       

      import

      mx.events.ListEvent;

       

       

      import

      mx.controls.Alert;

       

       

      import

      mx.collections.IViewCursor;

       

       

      import

      mx.controls.treeClasses.ITreeDataDescriptor;

       

       

      import

      mx.controls.treeClasses.TreeListData;

       

       

      import

      mx.controls.treeClasses.TreeItemRenderer;

       

       

      import

      mx.events.TreeEvent;

       

       

      import

      mx.collections.ArrayCollection;

       

       

      var locationAC:ArrayCollection = new

      ArrayCollection();

       

       

      public var treeLocationObj:Object = new

      Object();

       

       

      public var groupAC:ArrayCollection = new

      ArrayCollection();

       

       

      public var resourceAC:ArrayCollection = new

      ArrayCollection();

       

       

      // To load parent nodes like 'Locations', when we run project

       

       

      public function init():

      void

      {

       

       

      var locTreeObj1:Object = {label:"Jayanagar 7th Block", id:"1", children:new

      ArrayCollection()};

      locationAC.addItem(locTreeObj1);

       

       

      var locTreeObj2:Object = {label:"BTM Layout", id:"2", children:new

      ArrayCollection()};

      locationAC.addItem(locTreeObj2);

       

       

      var locTreeObj3:Object = {label:"Chennimalai", id:"3", children:new

      ArrayCollection()};

      locationAC.addItem(locTreeObj3);

      }

       

       

       

      //Get node object as TreeEvent

       

       

      private function addChildren(event:TreeEvent):

      void

      {

      treeLocationObj = event.item;

      getLocationId(event.item[

       

      "id"

      ]);

      }

       

       

       

      public function getLocationId(id:String):

      void

      {

       

       

      //To get depth of a particular node (treeLocationObj)

       

       

      var

      d:int = TreeListData(TreeItemRenderer(tree.itemToItemRenderer(treeLocationObj)).listData).depth;

       

       

      var

      str:String = TreeListData(TreeItemRenderer(tree.itemToItemRenderer(treeLocationObj)).listData).label;

       

       

      if

      (d == 1)

      {

       

       

      //Construct arraycollection

      groupAC =

       

      new

      ArrayCollection();

       

       

      var groupObj1:Object = {label:"IdeasGroup", id:"1", children:new

      ArrayCollection()};

      groupAC.addItem(groupObj1);

       

       

      var groupObj2:Object = {label:"ItrxGroup", id:"2", children:new

      ArrayCollection()};

      groupAC.addItem(groupObj2);

      loadChildGroup();

      }

       

       

      if

      (d == 2)

      {

      getGroupId(id);

      }

      }

       

       

       

      private function loadChildGroup():

      void

      {

      removeChildItems(treeLocationObj);

       

       

      for (var

      i:int=0; i < groupAC.length; i++)

      {

      tree.dataDescriptor.addChildAt(treeLocationObj, groupAC.getItemAt(i), 0, locationAC);

      }

      }

       

       

       

      public function getGroupId(id:String):

      void

      {

      resourceAC =

       

      new

      ArrayCollection();

       

       

      var resourceObj1:Object = {label:"Vinodha", id:"1"

      };

      resourceAC.addItem(resourceObj1);

       

       

      var resourceObj2:Object = {label:"Bhuvanesh", id:"2"

      }

      resourceAC.addItem(resourceObj2);

      addChildItem();

      }

       

       

       

      private function removeChildItems(parentNode:Object):

      void

      {

       

       

      var

      descriptor:ITreeDataDescriptor = tree.dataDescriptor;

       

       

      var

      childItems:Object;

       

       

      var

      cursor:IViewCursor;

       

       

      var

      hasChild:Boolean = descriptor.hasChildren(parentNode, locationAC);

       

       

      if

      (hasChild)

      {

      childItems = descriptor.getChildren(parentNode);

       

       

      if

      (childItems)

      {

      cursor = childItems.createCursor();

       

       

      while

      (!cursor.afterLast)

      {

      cursor.remove();

      }

      }

      }

      }

       

       

       

      private function addChildItem():

      void

      {

      removeChildItems(treeLocationObj);

       

       

      for (var

      i:int = 0; i< resourceAC.length; i++)

      {

      tree.dataDescriptor.addChildAt(treeLocationObj, resourceAC.getItemAt(i), 0, locationAC);

      }

      }

       

       

       

      private function removeItems(event:TreeEvent):

      void

      {

       

       

      //var ind:int = TreeListData(TreeItemRenderer(tree.itemToItemRenderer(event.item)).listData).rowIndex;

       

       

      var

      descriptor:ITreeDataDescriptor = tree.dataDescriptor;

       

       

      var

      childItems:Object;

       

       

      var

      cursor:IViewCursor;

       

       

      //var childItem:Object;

      childItems = descriptor.getChildren(event.item);

       

       

      if

      (childItems)

      {

      cursor = childItems.createCursor();

       

       

      while

      (!cursor.afterLast)

      {

       

       

      //childItem = cursor.current;

      cursor.remove();

      }

      }

      }

       

       

       

      public function getResourceID(event:ListEvent):

      void

      {

      Alert.show(

       

      "ID:"+ event.currentTarget.selectedItem["id"

      ]);

      Alert.show(

       

      "label:"+event.currentTarget.selectedItem["label"

      ]);

      }

      ]]>

      </mx:Script>

       

       

      <mx:Tree x="71" y="45" id="tree" dataProvider="{locationAC}

      " itemOpen="addChildren(event)"

      itemClose="removeItems(event)" itemClick="getResourceID(event)"

       

      ></mx:Tree>

       

      </mx:Application>