4 Replies Latest reply on Mar 16, 2007 5:04 PM by ntsiii

    Help trying to delete selected node tree.

    ctzn99
      Hello All,

      I'm hoping someone can give me some insight into what I'm trying to do. I've got a tree that gets populated through an xml data provider. Basically i'm building an XML editor for a very specific application and I want to be able to delete (or edit, change, etc) a selected node. My XML, at a high level, looks like the following (it may look familiar as I've posted other questions)

      <template>
      <tab label="General">
      <section label="Address">
      <field label="Street 1" />
      <field label="Street 2" />
      </section>
      <section label="Misc">
      <field label="Street 1" />
      </section>
      </tab>
      </template>

      I've build a delete function that works but its not very flexible and has several problems with it, not the least is that if I have two identical nodes (the "street 1" in the above example) it will delete the first one which isn't necessarilly the "selected" node in the tree. This is where I'm hoping for some pointers on how to make this better.

      private function alertResponse(event:CloseEvent):void
      {
      if(event.detail == Alert.YES)
      {
      var elementName:String = selectedNode.localName().toString();
      var elementLabel:String = selectedNode.@label;
      switch(elementName)
      {
      case "tab":
      delete templateXML.tab.(@label == elementLabel)[0];
      break;
      case "section":
      delete templateXML.tab.section.(@label == elementLabel)[0];
      break;
      case "field":
      delete templateXML.tab.section.field.(@label == elementLabel)[0];
      break;
      }
      }
      }

      Any insight, opinion, suggestions, comments, etc., would be appreciated.
        • 1. Re: Help trying to delete selected node tree.
          ntsiii Level 3
          Look at the xml editor example I have posted on cflex.net:
          http://www.cflex.net/showFileDetails.cfm?ObjectID=544&Object=File&ChannelID=1

          Perhpas you will see something you can use.

          Tracy
          • 2. Re: Help trying to delete selected node tree.
            ctzn99 Level 1
            Thanks Tracy,
            I finally had a chance to circle back to this and I downloaded your xml editor. While i didn't actually run it i did have a chance to peruse the code and sure enough your delete method was exactly what I was looking for.

            For anyone who happens upon this thread here's the solution:

            private function deleteSelectedNode():void
            {
            // get the selectedNode into XML
            var selectedNode:XML = XML(tree.selectedItem);

            // get a listCollection of the selectedNodes siblings, the list collection
            // is what provides us with the removeItemAt method.
            var siblingList:XMLListCollection = new XMLListCollection(selectedNode.parent().children());

            // figure out where the selectedNode is in relation to its siblings
            var selectedNodeIndext:int = siblingList.getItemIndex(selectedNode);

            // The "magic" line.
            siblingList.removeItemAt(selectedNodeIndex);
            }
            • 3. Re: Help trying to delete selected node tree.
              ctzn99 Level 1
              For discussion... it's curious that the XML object doesn't provide any sort of removeItemAt methods, and it doesn't seem like there's a good way to do it with e4x without using the "delete" keyword which, at least in my experience, is prone to complications.
              • 4. Re: Help trying to delete selected node tree.
                ntsiii Level 3
                "curious that the XML object doesn't provide any sort of removeItemAt "
                No kidding. When I fussed about this I was told it is because the e4x specification does not call for it.

                ""delete" keyword which, at least in my experience, is prone to complications"
                Double no kidding. It drives me nuts sometimes. But there it is. Remember the "Serenity Prayer".

                Tracy