Flex tree增加,刪除,查詢並定位節點

來源:互聯網
上載者:User

直接上代碼:

<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   xmlns:s="library://ns.adobe.com/flex/spark"   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"   ><fx:Script><![CDATA[import mx.collections.XMLListCollection;import mx.controls.Alert;[Bindable][Embed(source="images/open.png")]public var folderOpen:Class;[Bindable][Embed(source="images/close.png")]public var folderClose:Class;[Bindable][Embed(source="images/file.png")]public var fileInfo:Class;[Bindable]private var company:XML=<department>    <department name="部門A"><department name="小A" /><department name="小B" /></department><department name="部門B"><department name="小C"><department name="abc"><department name="efg"/></department><department name="opqr"/><department name="小D"><department name="ff"/></department></department>   </department></department>;[Bindable]private var companyData:XMLListCollection=new XMLListCollection(company.department);private function addNode():void{// 建立節點var newNode:XML=<employee/>;newNode.@name=empName.text;// 添加節點var xml:XML=XML(tree1.selectedItem);if (xml.length() > 0){xml[0].appendChild(newNode);tree1.expandChildrenOf(tree1.selectedItem,true);}}private function removeNode():void{if (tree1.selectedItem != null){var node:XML=XML(tree1.selectedItem);Alert.show("根節點不能刪除!","[錯誤]");//if(tree.dataDescriptor.root){///Alert.show("根節點不能刪除!","[錯誤]");//return;//}var nodeP:XML=node.parent();var childrenList:XMLListCollection=new XMLListCollection(XMLList(nodeP).children());var i:Number=childrenList.getItemIndex(node);childrenList.removeItemAt(i);if(childrenList.length == 0)tree1.selectedItem = nodeP;else{if(i == childrenList.length)i = i - 1;tree1.selectedItem = childrenList.getItemAt(i)}}else{Alert.show("請選中一個節點再進行刪除!");}}/* * 根據節點名字模糊查詢*/private function findNode(key:String):void {//descendants()返回除了根節點以外的所有節點,然後根據條件式篩選合格結果集var list:XMLList  = company.descendants().(@name.indexOf(key) != -1);expandParents(list[0]);tree1.selectedItem = list[0];}/* * 展開*/private function expandParents(xmlNode:XML):void {while (xmlNode.parent() != null) {  xmlNode = xmlNode.parent();tree1.expandItem(xmlNode,true, false);}}//展開所有private function expandAll():void {tree1.expandChildrenOf(tree1.dataProvider[0],true);}//收合所有private function closeAll():void {tree1.expandChildrenOf(tree1.dataProvider[0],false);}]]></fx:Script><s:layout><s:HorizontalLayout horizontalAlign="center" verticalAlign="middle"/></s:layout><s:Panel title="實現添加和刪除節點"  width="450"  height="450"  ><s:layout><s:HorizontalLayout/></s:layout><mx:Tree id="tree1"  dataProvider="{company}"  labelField="@name" height="300" showRoot="false" width="260" defaultLeafIcon="{fileInfo}" folderOpenIcon="{folderOpen}" folderClosedIcon="{folderClose}" /><mx:VBox><s:Button label="展開所有" click="expandAll()"/><s:Button label="關閉所有" click="closeAll()"/><mx:Button label="刪除節點" click="removeNode();"/><mx:HBox><s:TextInput id="empName"  width="60"  click="{empName.text = ''}"  prompt="新節點名"  /><mx:Button label="添加節點"   click="addNode();"/></mx:HBox><mx:HBox><s:TextInput id="keyName"  width="60"  prompt="關鍵字"/><mx:Button label="尋找節點"   click="findNode(keyName.text)"/></mx:HBox></mx:VBox></s:Panel></s:Application>

裡面有設定展開和關閉和檔案的圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.