Flex tree增加,删除,查询并定位节点

直接上代码:

 

<?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>

里面有设置展开和关闭和文件的图片

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值