增加和删除Tree节点

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" creationComplete="application1_creationCompleteHandler()"
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <s:layout>
        <s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import mx.rpc.events.ResultEvent;
            [Bindable]
            private var d:XML;
            
            protected function httpservice1_resultHandler(event:ResultEvent):void
            {
                d=event.result as XML;
                trace(d);
                t.dataProvider=d;
                
            }
            
            protected function application1_creationCompleteHandler():void
            {
                http.send();
                
            }
            
            protected function delNode():void
            {
                if(t.selectedIndex==-1){
                    return ;
                }else{
                    var node:XML=XML(t.selectedItem);
                    var child:XMLList=XMLList(node.parent().children());
                    for(var i:int=0;i<child.length();i++){
                        if((child[i].@value==node.@value)&&(node.@isBranch=true)){
                            delete child[i];
                        }
                    }
                }
            }
            
            protected function addNode():void
            {
                var newNode:XML=<Node/>;
                newNode.@value=textIn.text;
                var node:XML=XML(t.selectedItem);
                if(node.@isBranch==true){
                    node.parent().appendChild(newNode);
                }else{
                    node.appendChild(newNode);
                }
                textIn.text="";
            }
            
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        <s:HTTPService id="http" url="data/data.xml" resultFormat="e4x" result="httpservice1_resultHandler(event)" />
    </fx:Declarations>
    <mx:Tree id="t" labelField="@value" width="400" height="400"></mx:Tree>
    <s:HGroup width="400" height="40" verticalAlign="middle" horizontalAlign="center">
        <s:TextInput id="textIn"/>
        <s:Button label="增加节点" click="addNode()" enabled="{textIn.text==''?false:true}"/>
        <s:Button label="删除节点" click="delNode()" enabled="{t.selectedIndex==-1?false:true}"/>
    </s:HGroup>
</s:Application>

 

posted on 2012-09-11 11:37  雷、  阅读(357)  评论(0)    收藏  举报

导航