Hi All, I have a Simple Tree whose Data Provider is an ArrayCollection of Custom ActionScript Node Object (the code is listed below..)
When i add a new Child Node to any of the Nodes in the Tree i can get the parent of this Node without any problems.. How ever when i try to add a new Child Node for the Previously added Node i cannot get the Parent of the newly added Child Node..!!! Initial Tree: Root Node-1 1) Add a Child Element to Node-1 Root Node-1 ChildNode-1 Now i can get the parent of the ChildNode-1 2) Now add a new Child to childNode-1 Root Node-1 ChildNode-1 ChildOfChildNode-1 Now i cannot get the parent of ChildOfChildNode-1 !!! I am calling the getParent() method by passing the ChildNode and it returns null..(the same method above returns the correct parent ) Here is the code i am using.. TreeNode.as: [CODE] package test { import mx.collections.ArrayCollection; public class TreeNode { public function TreeNode() { } public var nodeId:String; public var nodeLabel:String; public var parentId:String; public var children:ArrayCollection; public var parent:Node; public function toString():String{ return "TreeNode[ nodeId="+nodeId+",nodeLabel="+nodeLabel+", parentId ="+parentId+"]"; } } } [/CODE] TreeTest.mxml: [CODE] <?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 test.ChangedNode; import mx.controls.Alert; import test.TreeNode; import mx.collections.ArrayCollection; private var sourceArrayCollection:ArrayCollection = null; private var contextNode:TreeNode = null; public function init():void{ trace("Here in init() of TreeDragDrop "); sourceArrayCollection = new ArrayCollection(); populateChildNodes("SourceTree",sourceArrayCollection); sourceCollectionTree.dataProvider = sourceArrayCollection; } private function populateChildNodes(nodeNamePrefix:String,collection:ArrayCollection):void{ trace("Here in populateChildNodes() "); for(var i:int =0;i<5;i++){ var node:TreeNode = new TreeNode(); node.nodeId =nodeNamePrefix+"-Id["+i+"]"; node.nodeLabel =nodeNamePrefix+"-Label["+i+"]"; node.children = new ArrayCollection(); var childNode:TreeNode = new TreeNode(); childNode.nodeId =nodeNamePrefix+"child-Id["+i+"]"; childNode.nodeLabel =nodeNamePrefix+"Child Label["+i+"]"; node.children.addItem(childNode); collection.addItem(node); } } public function treeLabel( item:Object ) : String{ var node:TreeNode =null; node = TreeNode(item); return node.nodeLabel; } private function getNodeInfo(event:Event):void{ trace("Here in getNodeInfo -------------------------------"); var selectedNode:TreeNode = Tree(event.target).selectedItem as TreeNode; trace("Selected Node ="+selectedNode); contextNode = selectedNode; } private function createAggregation():void{ aggregationNameDisplayBox.visible = true; aggregationNameTxt.setFocus(); } private function saveAggregationName():void{ trace("Here in saveAggregationName() and contextNode ="+contextNode); var newChildNode:TreeNode = null; var changedNode:ChangedNode = null; if(contextNode!=null){ newChildNode = new TreeNode(); newChildNode.nodeLabel=aggregationNameTxt.text; newChildNode.nodeId=Math.random().toString(); newChildNode.children = new ArrayCollection(); if(contextNode.children==null){ contextNode.children = new ArrayCollection(); } newChildNode.parentId = contextNode.nodeId; contextNode.children.addItem(newChildNode); trace("After Adding the Child Node..and childNode ="+newChildNode); var parentTarget :TreeNode = sourceCollectionTree.getParentItem(newChildNode); trace("Parent of the Child Node ="+parentTarget); aggregationNameDisplayBox.visible = false; aggregationNameTxt.text =""; trace("Expnad the Context Node..."+contextNode); expandContextNode(); } else{ Alert.show("Please select a Context Node First"); } } private function expandContextNode():void{ trace("Here in expandContextNode and contextTree ="+sourceCollectionTree+" && contextNode ="+contextNode); var argsArray:Array = new Array(2); argsArray[0]=this.contextNode; argsArray[1]= true; if(sourceCollectionTree!=null){ sourceCollectionTree.callLater(sourceCollectionTree.expandChildrenOf,argsArray); } else{ trace("Context Tree is NULL in expandContextNode() !!!!"); } } ]]> </mx:Script> <mx:VBox> <mx:HBox> <mx:Button id="createAggregationBtn" click="createAggregation()" label="Aggregation" toolTip="Create Aggregation"/> <mx:HBox id="aggregationNameDisplayBox" visible="false"> <mx:Label text="Name Display"/> <mx:TextInput id="aggregationNameTxt"> </mx:TextInput> <mx:Button id="submitAggregation" label="Go" click="saveAggregationName()"/> </mx:HBox> </mx:HBox> <mx:HBox width="100%" height="90%"> <mx:Tree id="sourceCollectionTree" labelFunction="treeLabel" showRoot="true" width="100%" height="100%" change="getNodeInfo(event)" /> </mx:HBox> </mx:VBox> </mx:Application> [/CODE] Once i add a child to a node the tree's dataProvider is getting updated internally (i am not making an explicit call.. Do i need to call a method to refresh the tree dataProvider ?? I checked the valiedateNow() but i guess they are for checking whether the UI needs a repaint .. Thanks Mars..