Flex中树的添加,删除操作Fl

Flex中树的数据就是一个XML,所以操作类似XML的操作

 

 

代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="413" height="344">
    
<mx:Script>
        
<![CDATA[
            import mx.collections.XMLListCollection;
            import mx.controls.Alert;
            
            [Bindable]
            private var company:XML =
              <list>
                <department title="Finance" code="200">
                    <employee name="John H"/>
                    <employee name="Sam K"/>
                </department>
                <department title="Operations" code="400">
                    <employee name="Bill C"/>
                    <employee name="Jill W"/>
                </department>                    
                <department title="Engineering" code="300">
                    <employee name="Erin M"/>
                    <employee name="Ann B"/>
                </department>                                
              </list>;
              
            [Bindable]
            private var companyData:XMLListCollection = 
                new XMLListCollection(company.department);
            
            /**
            * Tree里的显示函数
            * */
            private function treeLabel(item:Object):String {
                var node:XML = XML(item);
                if( node.localName() == "department" )
                    return node.@title;
                else
                    return node.@name;
            }

            private function addItem():void {
                if (tree.selectedItem==null){
                    Alert.show("请选择一个节点");
                }
                var node:XML = XML(tree.selectedItem);//.parent();
                var newNode:XML = <employee/>;
                newNode.@name = empName.text;
                node.appendChild(newNode);
            }

            private function removeItem():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[i].@name == node.@name ) {
                        delete children[i];
                    }
                }
            }
        
]]>
    
</mx:Script>
    
    
<mx:Tree id="tree" 
        top
="72" left="50" 
        dataProvider
="{companyData}"
        labelFunction
="treeLabel"
        height
="225" width="300"
    
/>
    
    
<mx:VBox>
        
<mx:HBox>           
            
<mx:Button label="添加" click="addItem();"/>
            
<mx:TextInput id="empName"/>            
        
</mx:HBox>
        
<mx:Button label="Remove Selected Employee" click="removeItem();"/>                     
    
</mx:VBox>

    
</mx:Application>


posted on 2010-01-06 17:26  happyli  阅读(435)  评论(0)    收藏  举报

导航