flex 树型结构右键菜单和图标

本文介绍了一种使用Adobe Flex框架创建树形菜单的方法。通过定义XML数据结构来组织菜单项,并利用Flex的Tree组件和自定义脚本来实现树形结构的展开、图标设置及右键菜单等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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"
			   creationComplete="init()">
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			
			private var iconArr:Array = new Array();
			private var menuItem:ContextMenuItem;
			
			[Embed(source="/assets/images/Buttonclose.png")]
			[Bindable]
			private var icon_oa:Class;
			
			[Embed(source="/assets/images/Buttonclose.png")]
			[Bindable]
			private var icon_1:Class;
			
			[Embed(source="/assets/images/Buttonclose.png")]
			[Bindable]
			private var icon_3:Class;
			
			[Embed(source="/assets/images/Buttonclose.png")]
			[Bindable]
			private var icon_2:Class;
			
			[Embed(source="/assets/images/Buttonclose.png")]
			[Bindable]
			private var icon_4:Class;
			
			[Embed(source="/assets/images/Buttonclose.png")]
			[Bindable]
			private var icon_5:Class;
			
			[Bindable]
			private var xml:XML=
				<node icon="oa" label="办公自动化">
					<node label="公文管理">
						<node id="1" icon="icon1" label="公文收发" />
						<node id="2" icon="icon1" label="公文归档"/>
						<node id="3" icon="icon1" label="公文查询"/>
						<node id="4" icon="icon1" label="通知公告"/>
					</node>
					<node label="人事管理">
						<node label="历史数据查询">
							<node id="5" icon="icon2" label="离职人员查询"/>
						</node>
						<node label="新员工入职">
							<node id="6" icon="icon3" label="基本信息"/>
							<node id="7" icon="icon3" label="员工转正"/>
						</node>
						<node label="工资管理">
							<node id="8" icon="icon4" label="设置公式"/>
							<node id="9" icon="icon4" label="工资发放"/>
						</node>
					</node>
					<node label="决策支持">
						<node id="11" icon="icon5" label="规则设置"/>
					</node>
				</node>
			
			private function init():void{
				iconArr["oa"]=icon_oa;
				iconArr["icon1"]=icon_1;
				iconArr["icon2"]=icon_2;
				iconArr["icon3"]=icon_3;
				iconArr["icon4"]=icon_4;
				iconArr["icon5"]=icon_5;
				this.expendAllTreeNode();
				this.addTreeMenu();
				tree.contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT,rightClickHandler);
			}
			
			/*
			* 展开树
			*/
			public function expendAllTreeNode(): void {
				tree.validateNow();
				tree.selectedIndex = 0;
				tree.expandChildrenOf(tree.selectedItem,true);
			}
			
			/*
			* 添加右键菜单
			*/
			public function addTreeMenu():void{
				var menu:ContextMenu=new ContextMenu();
				menu.hideBuiltInItems();
				menuItem=new ContextMenuItem("打开");
				menuItem.visible=false;
				menu.customItems.push(menuItem);
				tree.contextMenu=menu;
			}
			
			/*
			* 设置节点图标
			*/
			private function setIcon(item:Object):Class{
				var node:XML = item as XML;
				var nodeIcon:String = node.@icon;
				if(node!=null && nodeIcon!=null && nodeIcon.length!=0)
					return iconArr[nodeIcon];
				else
					return null;
			}
			
			/*
			* 处理右键事件
			*/
			private function rightClickHandler(ev:ContextMenuEvent):void{
				menuItem.visible=false;
				var tag:Object=ev.mouseTarget;
				//右键时行选中,此处需捕获异常
				try{
					if(tag && tag.parent && tag.parent.data && (tag.parent.data is XML))
						tree.selectedItem = tag.parent.data;
				}catch(ex:Error){
				}
				
				var node:XML = tree.selectedItem as XML;
				if(node!=null && node.@id!=null){
					var nodeId:String = node.@id;
					if(nodeId.length>0)
						menuItem.visible = true;
				}
			}
		]]>
	</fx:Script>
	<s:HGroup width="30%" height="100%">
		<mx:Tree id="tree" width="100%" height="100%" dataProvider="{xml}"
				 labelField="@label" iconFunction="setIcon" />
	</s:HGroup>
</s:Application>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值