博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flex4_Tree组件1(添加、删除、展开、关闭、右键菜单)
阅读量:6417 次
发布时间:2019-06-23

本文共 5731 字,大约阅读时间需要 19 分钟。

1、屏蔽系统菜单:工程目录“html-template”文件夹-->“index.template.html”文件中,在var params = {};语句下添加新语句:

        params.wmode = "opaque";

2、新建Tree右键菜单工具类:TreeRightClickManager.as

package com.th.rightClickManager{    import flash.display.InteractiveObject;    import flash.events.ContextMenuEvent;    import flash.events.MouseEvent; import flash.external.ExternalInterface; import mx.core.Application; import mx.core.FlexGlobals; public class TreeRightClickManager { static private var rightClickTarget:*; static public const RIGHT_CLICK:String = "rightClick"; static private const javascript:XML = ; public function TreeRightClickManager() { return; } static public function regist() : Boolean { if (ExternalInterface.available) { ExternalInterface.call(javascript, ExternalInterface.objectID); ExternalInterface.addCallback("rightClick", dispatchRightClickEvent); //Application.application.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler); FlexGlobals.topLevelApplication.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler); } return true; } static private function mouseOverHandler(event:MouseEvent) : void { rightClickTarget = InteractiveObject(event.target); return; } static private function dispatchRightClickEvent() : void { var event:ContextMenuEvent; if(rightClickTarget !=null) { event =new ContextMenuEvent(RIGHT_CLICK,true,false, rightClickTarget as InteractiveObject, rightClickTarget as InteractiveObject); rightClickTarget.dispatchEvent(event); } return; } } }

3、Flex文件

; if (!rightClickRegisted){ TreeRightClickManager.regist(); rightClickRegisted = true; } //加载右击事件 studentTree.addEventListener(TreeRightClickManager.RIGHT_CLICK,treeRightClickHandler); } //树右击事件 private function treeRightClickHandler(event:ContextMenuEvent):void { tree_onRightClicked(event); tree_removeMenu(); tree_InitMenu(); } private function tree_onRightClicked(e:ContextMenuEvent):void { var rightClickItemRender:IListItemRenderer; var rightClickIndex:int; if(e.mouseTarget is IListItemRenderer) { rightClickItemRender = IListItemRenderer(e.mouseTarget); } else if(e.mouseTarget.parent is IListItemRenderer) { rightClickItemRender = IListItemRenderer(e.mouseTarget.parent); } if(rightClickItemRender != null) { rightClickIndex = studentTree.itemRendererToIndex(rightClickItemRender); if(studentTree.selectedIndex != rightClickIndex) { studentTree.selectedIndex = rightClickIndex; } } } //删除右键菜单 private function tree_removeMenu():void { if(menu!=null) { menu.hide(); menu.removeEventListener(MenuEvent.ITEM_CLICK,tree_MenuItemSelected); menu=null; } } //生成右键菜单 private var selectedId:String=""; private var selectedLabel:String=""; private function tree_InitMenu():void { var currentItem:XML=studentTree.selectedItem as XML; selectedId=currentItem.@id; selectedLabel=currentItem.@label; var lx:String=currentItem.@lx; menu = Menu.createMenu(this, tree_createMenuItems(lx), false); menu.iconField="itemIcon";//右键菜单的图标 menu.labelField="label"; //右键菜单的名称 menu.variableRowHeight = true; menu.addEventListener(MenuEvent.ITEM_CLICK, tree_MenuItemSelected); //右键菜单的事件 var point:Point = new Point(mouseX,mouseY); point = localToGlobal(point); menu.show(point.x,point.y); //显示右键菜单 } //根据节点不同,生成不同的菜单 private function tree_createMenuItems(lx:String):Array { var menuItems:Array = new Array(); var menuXs:Object= new Object; menuXs.label = '刷新'; //menuXs.itemIcon = this.menu_TZ_add; //给菜单加图标 menuItems.push(menuXs); var menuExpand:Object=new Object(); menuExpand.label="展开当前节点"; menuItems.push(menuExpand); var menuClose:Object=new Object(); menuClose.label="关闭当前节点"; menuItems.push(menuClose); var menuExpandAll:Object=new Object(); menuExpandAll.label="展开所有节点"; menuItems.push(menuExpandAll); var menuCloseAll:Object=new Object(); menuCloseAll.label="关闭所有节点"; menuItems.push(menuCloseAll); switch(lx){ case "0": var menuAddNj:Object = new Object; menuAddNj.label = "添加年级"; menuItems.push(menuAddNj); var menuDelNj:Object=new Object(); menuDelNj.label="删除年级"; menuItems.push(menuDelNj); break; case "1": var menuAddBj:Object=new Object(); menuAddBj.label="添加班级"; menuItems.push(menuAddBj); var menuDelBj:Object=new Object(); menuDelBj.label="删除班级"; menuItems.push(menuDelBj); var menuAddStu:Object=new Object(); menuAddStu.label="添加学生"; menuItems.push(menuAddStu); break; case "2": var menuEditStu:Object=new Object(); menuEditStu.label="修改学生"; menuItems.push(menuEditStu); var menuDelStu:Object=new Object(); menuDelStu.label="删除学生"; menuItems.push(menuDelStu); break; } return menuItems; } //点击菜单,相应方法 private function tree_MenuItemSelected(event:MenuEvent):void { var menuItem:Object = event.menu.selectedItem as Object; switch(menuItem.label) { case "展开当前节点": nodeExpand(); break; case "关闭当前节点": nodeClose(); break; case "展开所有节点": nodeExpandAll(); break; case "关闭所有节点": nodeCloseAll(); break; case "删除学生": delStuNode(); break]]>

转载地址:http://njpra.baihongyu.com/

你可能感兴趣的文章
修复 Ubuntu 14.04 从待机中唤醒后鼠标键盘出现僵死情况
查看>>
ES6 的功能侦测库 ES-Checker
查看>>
从DML角度看ADS和ORACLE的区别
查看>>
ruby on rails 入门注意事项
查看>>
如何写gdb命令脚本
查看>>
小心使用DCOM
查看>>
Android ListView展示不同的布局
查看>>
oracle 表(下)
查看>>
iOS宏(自己使用,持续更新)
查看>>
瀑布流效果
查看>>
手把手玩转win8开发系列课程(3)
查看>>
NGINX引入线程池 性能提升9倍
查看>>
《淘宝技术这十年》读书笔记 (四). 分布式时代和中间件
查看>>
linux下mongodb定时备份指定的集合
查看>>
GNU Radio中流图的动态配置
查看>>
SMP架构多线程程序的一种性能衰退现象—False Sharing
查看>>
choose MariaDB 10 or 5.x
查看>>
oVirt JBAS server start failed, ajp proxy cann't server correct. ovirt-engine URL cann't open
查看>>
CDP WebConsole上线公告
查看>>
ubuntu下安装摄像头应用程序xawtv
查看>>