天天看點

flex tree的展開,關閉,添加、删除子節點

/*=========flex tree的展開,關閉,添加、删除子節點=========*/
/*=========因為用的靜态資料,是以并沒有真正的删除=========*/
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="flexlib.controls.*" xmlns:ns2="flexlib.containers.*" width="100%" height="100%" xmlns:commont="commont.*">
 <mx:Tree x="10" y="10" width="199" height="327" labelField="@label" id="tree" 
  creationComplete="load()" fontSize="14"></mx:Tree>

  <mx:XMLList id="treeData">
        <node label="Mail Box" id="1">
            <node label="Inbox" id="101">
                <node label="Marketing" id="10101"/>
                <node label="Product Management" id="10102"/>
                <node label="Personal" id="10103"/>
            </node>
            <node label="Outbox" id="2">
                <node label="Professional" id="201"/>
                <node label="Personal" id="202"/>
            </node>
            <node label="Spam" id="3"/>
            <node label="Sent" id="4"/>
        </node>    
    </mx:XMLList>

 <mx:Script>
  <![CDATA[
   import mx.controls.Alert;
   //加載樹資料源
   private function load():void{
    tree.dataProvider=treeData;

   }
   //展開所有節點
   private function expandAll():void{
    tree.expandChildrenOf(tree.selectedItem,true);

   }
   //關閉所有節點
   private function closeAll():void{
    tree.openItems=[];
   }
   //添加子節點
   private function addNode():void{
    var xml:XML=tree.selectedItem as XML;
    xml.appendChild("hello");

   }
   //删除子節點
   private function delNode():void{
    tree.dataDescriptor.removeChildAt(tree.selectedItem.parent(),tree.selectedItem,tree.selectedItem.childIndex(),tree.dataProvider);
   }

  ]]>
 </mx:Script>
 <mx:Button x="242" y="28" label="添加節點" fontSize="14" click="addNode()"/>
 <mx:Button x="242" y="69" label="删除節點" fontSize="14" click="delNode()"/>
 <mx:Button x="242" y="115" label="展開節點" fontSize="14" click="expandAll()"/>
 <mx:Button x="242" y="156" label="收回節點" fontSize="14" click="closeAll()"/>
</mx:Application>
           

本文來自CSDN部落格,轉載請标明出處:http://blog.csdn.net/xuhuanchao/archive/2009/10/21/4706715.aspx