2 Replies Latest reply on Feb 6, 2007 3:18 AM by rkschulz

    Help on Deleting Node in Tree?

    Pigg_Man Level 1
      I'm trying to delete a node on a tree and some weird stuff is occurring. Here is my code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="init();" layout="absolute">
      <mx:Script>
      <![CDATA[

      private function click () : void {
      if (MyTree.selectedItems.length > 0) {
      var items:Array = MyTree.selectedItems;
      for (var i:int = 0; i < items.length; i++) {
      var nodeToDelete:XML = XML(items );
      var xlcParent:XMLListCollection = new XMLListCollection(nodeToDelete.parent().children());
      var iIndex:int = xlcParent.getItemIndex(nodeToDelete);
      xlcParent.removeItemAt(iIndex);
      }
      }
      }
      ]]>
      </mx:Script>
      <mx:XMLListCollection id="MyDP">
      <mx:XMLList>
      <root label="Default Policy">
      <node label="node1"/>
      <node label="node2"/>
      <node label="node3"/>
      <node label="node4"/>
      <node label="node5">
      <node label="node1"/>
      <node label="node2"/>
      <node label="node3"/>
      <node label="node4"/>
      <node label="node5">
      <node label="node1"/>
      <node label="node2"/>
      <node label="node3"/>
      <node label="node4"/>
      </node>
      </node>
      </root>
      </mx:XMLList>
      </mx:XMLListCollection>

      <mx:Tree labelField="@label" id="MyTree" dataProvider="{MyDP}" x="141" y="109" width="528" height="440"/>
      <mx:Button click="click();" x="141" y="557" label="Delete Selected Node"/>
      </mx:Application>


      For some reason when I expand all the nodes and try to delete one of the great grand children all of the great grand children shift over as if they are siblings of the node that I deleted the great grand children from.

      Anyone have some good ideas? I sure wish deletion of nodes was as easy as in actionscript 2.0
        • 1. Re: Help on Deleting Node in Tree?
          Pigg_Man Level 1
          I found that if you select the parent node after deletion that it fixes this problem. This is a major bug!!!
          • 2. Re: Help on Deleting Node in Tree?
            rkschulz Level 1
            There a quite a few posts about this problem here already - it seems to be a tree rendering bug (seems to come in various different flavours).

            The safest work around after fiddling with the nodes in the tree is to get the axe out and reassign the data provider. To be nice to the user, you might want to keep the expansion state (this may not work if you made big "structural" changes to the tree).

            The following works for me (after your changed the tree data model):

            var openItems:Object = treeopenItems;
            tree.dataProvider = tree.dataProvider;
            this.openItems = openItems;

            Hope this helps.

            Robert.