5 Replies Latest reply on Jan 26, 2007 9:17 AM by Pigg_Man

    removing a node in a tree

    Pigg_Man
      Im using this line of code
      var selectedNode:XML = XML(MyTree.selectedItem);
      and i want to remove the selectedNode from the tree... The only way that I have found to remove the node is to use these lines of code which seems ridiculous just to remove the node
      var parentChildren:XMLList = XMLList(selectedNode.parent().children());
      for (var i:int = 0; i < parentChildren.length(); i++) {
      if (parentChildren .@label == selectedNode.@label) {
      delete parentChildren
      ;
      }
      }

      The only thing that I have found is to use the XMLNode object because it has removeNode method... Except I can't use that either because I get a warning when I try to cast the selectedItem into a XMLNode.

      Does anyone know how to simplify a deletion of a node.
        • 1. Re: removing a node in a tree
          ntsiii Level 3
          First, XMLNode is an object belonging to the old flash xml style xml stuff, and is not part of the new e4x XML class. Neither is XMLDocument. In the new XML, yo only have XML and XMLList to work with.

          As far as delete, it is a bit of a PITA. I am amazed that we have to use the top-level object delete, but that is the case.

          Now you should be able to just do:
          delete selectedNode;

          Try that first..However, looking back at my own code I didn't do this. Instead, I use this function:
          //deletes the currently selected node
          private function deleteSelectedNode():void
          {
          var nodeToDelete:XML = XML(treeData.selectedItem); //reference to the node to delete
          var xlcParent:XMLListCollection = new XMLListCollection(nodeToDelete.parent().children()); //collection of nodes containing the node to delete
          var iIndex:int = xlcParent.getItemIndex(nodeToDelete); //index of node to delete in collection
          xlcParent.removeItemAt(iIndex); //remove the node
          //treeData.invalidateList(); //refresh the tree
          }//deleteSelectedNode

          Maybe that technique will work for you.

          Tracy
          • 2. Re: removing a node in a tree
            Level 7
            First, XMLNode is an object belonging to the old flash xml style xml stuff,
            and is not part of the new e4x XML class. Neither is XMLDocument. In the new
            XML, yo only have XML and XMLList to work with.

            As far as delete, it is a bit of a PITA. I am amazed that we have to use the
            top-level object delete, but that is the case.

            Now you should be able to just do:
            delete selectedNode;

            Try that first..However, looking back at my own code I didn't do this.
            Instead, I use this function:
            //deletes the currently selected node
            private function deleteSelectedNode():void
            {
            var nodeToDelete:XML = XML(treeData.selectedItem); //reference to the
            node to delete
            var xlcParent:XMLListCollection = new
            XMLListCollection(nodeToDelete.parent().children()); //collection of nodes
            containing the node to delete
            var iIndex:int = xlcParent.getItemIndex(nodeToDelete); //index of node to
            delete in collection
            xlcParent.removeItemAt(iIndex); //remove the node
            //treeData.invalidateList(); //refresh the tree
            }//deleteSelectedNode

            Maybe that technique will work for you.

            Tracy

            • 3. Re: removing a node in a tree
              ntsiii Level 3
              Hee is a link to an example that does a lot of work modifying a Tree:
              http://www.cflex.net/showfiledetails.cfm?ChannelID=1&Object=File&objectID=544

              Tracy
              • 5. Re: removing a node in a tree
                Pigg_Man Level 1
                I downloaded the .changetozip file and changed it to a zip... and it hoses up my computer