0 Replies Latest reply on Feb 23, 2011 12:44 AM by HadanMarv

    Load tree using renderer

    HadanMarv

      Hi,

       

      My current application contains a <mx:tree>

      his dataprovider is an xml file loaded from a httpservice

      this tree have a renderer extends TreeItemRenderer that allow me to add checkbox when node doesn't have childs.

       

      When user check or uncheck a node, i put some information in a datagrid.

      This datagrid is finally export to a file.

       

      Now the question is how can i initialize the tree using the two file (the original and this saved) ?

       

      See below my renderer code :

       

      package manageCheckBox
      {
      import flash.events.Event;
      import flash.events.MouseEvent;

       

      import mx.collections.IViewCursor;
      import mx.controls.Alert;
      import mx.controls.CheckBox;
      import mx.controls.DataGrid;
      import mx.controls.treeClasses.*;
      import mx.controls.treeClasses.TreeItemRenderer;

       

      public class DisplayCheckBox extends TreeItemRenderer{
      public var chk:CheckBox;
      public var itemXml:XML; // original xml file
      public var dg:DataGrid; // datagrid to put information on
      public var myFields:XML; // saving file
      public function DisplayCheckBox(){
      super();
      mouseEnabled = false;
      }

       

      override public function set data(value:Object):void{
      if(value != null){
      super.data = value;
      this.itemXml = XML(value);
      if(this.itemXml.@checked == "1"){
      this.chk.selected = true;
      }else{
      this.chk.selected = false;
      }
      }
      }
      override protected function createChildren():void{
      super.createChildren();
      chk = new CheckBox();
      chk.addEventListener(MouseEvent.CLICK, handleChkClick);
      addChild(chk);
      }

       

      override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
      super.updateDisplayList(unscaledWidth,unscaledHeight);
      if(super.data){
      var tld:TreeListData = TreeListData(super.listData);
      if(tld.hasChildren){
      this.chk.visible = false;
      }else{
      this.chk.visible = true;
      }
      if(chk.visible){
      this.chk.x = super.label.x
      super.label.x = this.chk.x + 17;
      this.chk.y = super.label.y+8;
      }
      }
      }

       

      private function handleChkClick(evt:MouseEvent):void{
      if(this.chk.selected){
      this.itemXml.@checked = "1";
      // put information on the datagrid
      dg.dataProvider.addItem(
      {
      "col1": this.itemXml.@label,
      "col2": this.itemXml.@label,
      "col3": this.itemXml.@resourceId
      }
      );
      }else{
      //this.checked = false;
      this.itemXml.@checked = "0";
      var vObject:Object = dg.dataProvider ;
      var cursor:IViewCursor=vObject.createCursor();
      while( !cursor.afterLast )
      {
      if(cursor.current.col1 == this.itemXml.@label) {
      cursor.remove() ;
      }
      cursor.moveNext();
      }
      }
      }
      }
      }

       

      Thanks in advanc for your reply,

      Regards,

       

      HadanMarv