4 Replies Latest reply on Jan 29, 2009 5:50 AM by Newsgroup_User

    XML Help

    manteratech
      Im having a hell of a time getting a Tree to work by adding nodes. I can add nodes, but not to the correct location that I need them in the XML. Each time I do an itemOpen I want to add nodes to the node I just expanded, but I keep getting an error stating "The appendChild method only works on lists containing one item". WTF, new to me, Im sure there is a good reason for it. Is there a better way I can accomplish this? I intend on using this with a cfc to load from a db, any help would be appreciated.


      CODE
      ===========================================================================
      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initBins();">
      <mx:Script>
      <![CDATA[
      import mx.events.TreeEvent;
      import mx.collections.XMLListCollection;
      import mx.controls.Alert;

      [Bindable]
      private var MyBins:XML = <MyBins><bin id="0" title="My Documents"/></MyBins>;

      private var binArr:Array = ["Office","Reports","New Accounts"];

      [Bindable]
      private var openItems:Object = new Object();

      [Bindable]
      private var BinData:XMLListCollection =
      new XMLListCollection(MyBins.bin);

      private function treeLabel(item:Object):String {
      var node:XML = XML(item);
      if( node.localName() == "Bin" )
      return node.@title;
      else
      return node.@title;
      }


      private function initBins():void
      {
      var i = 10;
      for each(var s:String in binArr)
      {
      var newNode:XML = <bin/>;
      newNode.@title = s;
      newNode.@isBranch = "true";
      newNode.@id = i;
      i++;
      var tmpBins:XMLList = MyBins.bin;
      if( tmpBins.length() > 0 ) {
      tmpBins[0].appendChild(newNode);
      //empName.text = "";
      }
      openItems = tree.openItems;
      refreshTree(openItems);
      }
      }

      private function refreshTree(openNodes:Object):void
      {
      tree.openItems = openNodes;
      tree.dataProvider = BinData;
      }
      /*private function addEmployee():void {
      var newNode:XML = <employee/>;
      newNode.@name = empName.text;
      newNode.@isBranch = "true";
      var dept:XMLList =company.department.(@title == "Operations");
      if( dept.length() > 0 ) {
      dept[0].appendChild(newNode);
      empName.text = "";
      }
      }*/
      private function expandBin(e:TreeEvent):void
      {

      //Alert.show(e.item.@title);
      var newNode:XML = <bin/>;
      newNode.@title = 'Whatever';
      newNode.@isBranch = "true";
      newNode.@id = 25;
      var tmpBins:XMLList = MyBins.bin;
      tmpBins.(@id == e.item.@id).appendChild(newNode);
      //empName.text = "";

      }

      private function addBin():void {
      var newNode:XML = <bin/>;
      newNode.@title = 'Whatever';
      newNode.@isBranch = "true";
      var tmpBins:XMLList = MyBins.bin.(@title == "My Documents");
      if( tmpBins.length() > 0 ) {
      tmpBins[0].appendChild(newNode);
      //empName.text = "";
      }
      }

      private function removeEmployee():void {
      var node:XML = XML(tree.selectedItem);
      if( node == null ) return;
      if( node.localName() != "employee" ) return;

      var children:XMLList = XMLList(node.parent()).children();
      for(var i:Number=0; i < children.length(); i++) {
      if( children .@name == node.@name ) {
      delete children
      ;
      }
      }
      }
      ]]>
      </mx:Script>

      <mx:Tree id="tree"
      top="72" left="50"
      dataProvider="{BinData}"
      labelFunction="treeLabel" itemOpen="expandBin(event);"
      height="225" width="300"
      />

      <mx:VBox>
      <mx:HBox>
      <mx:Button label="Add Operations Employee" click="addBin();"/>
      <mx:TextInput id="empName"/>
      </mx:HBox>
      <mx:Button label="Remove Selected Employee" click="removeEmployee();"/>
      </mx:VBox>
      </mx:WindowedApplication>