加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex中tree实现的种种细节

发布时间:2020-12-15 05:13:46 所属栏目:百科 来源:网络整理
导读:? 需要解决的问题: 1、自定义节点icon图标 2、右键菜单 3、 右键时行选中 (比较图1和图2) 4、 父节点右键取消菜单 (比较图3和图4) ? Java代码 ? ?xml?version= "1.0" ?encoding= "utf-8" ? ?? s:Application?xmlns:fx= "http://ns.adobe.com/mxml/2009"
?

需要解决的问题:

1、自定义节点icon图标

2、右键菜单

3、右键时行选中(比较图1和图2)

4、父节点右键取消菜单(比较图3和图4)

?

Java代码

?

  1. <?xml?version="1.0"?encoding="utf-8"?> ??
  2. <s:Application?xmlns:fx="http://ns.adobe.com/mxml/2009"? ??
  3. ???????????????xmlns:s="library://ns.adobe.com/flex/spark"? ??
  4. ???????????????xmlns:mx="library://ns.adobe.com/flex/mx"? ??
  5. ???????????????creationComplete="init()"> ??
  6. ????<fx:Declarations> ??
  7. ????????<!--?Place?non-visual?elements?(e.g.,?services,?value?objects)?here?--> ??
  8. ????</fx:Declarations> ??
  9. ????<fx:Script> ??
  10. ????????<![CDATA[ ??
  11. ????????????import?mx.controls.Alert; ??
  12. ???????????? ??
  13. ????????????private?var?iconArr:Array?=?new?Array(); ??
  14. ????????????private?var?menuItem:ContextMenuItem; ??
  15. ???????????? ??
  16. ????????????[Embed(source="../assets/images/home_nav.gif")] ??
  17. ????????????[Bindable] ??
  18. ????????????private?var?icon_oa:Class; ??
  19. ???????????? ??
  20. ????????????[Embed(source="../assets/images/text.gif")] ??
  21. ????????????[Bindable] ??
  22. ????????????private?var?icon_1:Class; ??
  23. ???????????? ??
  24. ????????????[Embed(source="../assets/images/heap_view.gif")] ??
  25. ????????????[Bindable] ??
  26. ????????????private?var?icon_3:Class; ??
  27. ???????????? ??
  28. ????????????[Embed(source="../assets/images/find.gif")] ??
  29. ????????????[Bindable] ??
  30. ????????????private?var?icon_2:Class; ??
  31. ???????????? ??
  32. ????????????[Embed(source="../assets/images/readwrite_obj.gif")] ??
  33. ????????????[Bindable] ??
  34. ????????????private?var?icon_4:Class; ??
  35. ???????????? ??
  36. ????????????[Embed(source="../assets/images/speak.gif")] ??
  37. ????????????[Bindable] ??
  38. ????????????private?var?icon_5:Class; ??
  39. ???????????? ??
  40. ???????????? ??
  41. ????????????[Bindable] ??
  42. ????????????private?var?xml:XML= ??
  43. ????????????????<node?icon="oa"?label="办公自动化">??????????????????????????? ??
  44. ????????????????????<node?label="公文管理">??????????? ??
  45. ????????????????????????<node?id="1"?icon="icon1"?label="公文收发"?/>????????? ??
  46. ????????????????????????<node?id="2"?icon="icon1"?label="公文归档"/>???????? ??
  47. ????????????????????????<node?id="3"?icon="icon1"?label="公文查询"/>??????????? ??
  48. ????????????????????????<node?id="4"?icon="icon1"?label="通知公告"/>??????????? ??
  49. ????????????????????</node>??????????????????????????????????????????????????? ??
  50. ????????????????????<node?label="人事管理">??????????????????????????????????? ??
  51. ????????????????????????<node?label="历史数据查询">????????????????????????????? ??
  52. ????????????????????????????<node?id="5"?icon="icon2"?label="离职人员查询"/>?? ??
  53. ????????????????????????</node>????????????????????????????????????????????????? ??
  54. ????????????????????????<node?label="新员工入职">????????????????????????????? ??
  55. ????????????????????????????<node?id="6"?icon="icon3"?label="基本信息"/>? ??
  56. ????????????????????????????<node?id="7"?icon="icon3"?label="员工转正"/>? ??
  57. ????????????????????????</node>????????????????????????????????????????????????? ??
  58. ????????????????????????<node?label="工资管理">????????????????????????????????? ??
  59. ????????????????????????????<node?id="8"?icon="icon4"?label="设置公式"/>??????? ??
  60. ????????????????????????????<node?id="9"?icon="icon4"?label="工资发放"/>??????? ??
  61. ????????????????????????</node>????????????????????????????????????????????????????????? ??
  62. ????????????????????</node>??????????????????????????????????????????????????? ??
  63. ????????????????????<node?label="决策支持">??????????????????????????????????????? ??
  64. ????????????????????????<node?id="11"?icon="icon5"?label="规则设置"/>?????????????? ??
  65. ????????????????????</node>??????????????????????????????????????????????????? ??
  66. ????????????????</node> ??
  67. ???????????????? ??
  68. ????????????private?function?init():void{ ??
  69. ????????????????iconArr["oa"]=icon_oa; ??
  70. ????????????????iconArr["icon1"]=icon_1; ??
  71. ????????????????iconArr["icon2"]=icon_2; ??
  72. ????????????????iconArr["icon3"]=icon_3; ??
  73. ????????????????iconArr["icon4"]=icon_4; ??
  74. ????????????????iconArr["icon5"]=icon_5; ??
  75. ????????????????this.expendAllTreeNode(); ??
  76. ????????????????this.addTreeMenu(); ??
  77. ????????????????tree.contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT,rightClickHandler); ??
  78. ????????????} ??
  79. ???????????????? ??
  80. ????????????/* ?
  81. ?????????????*?展开树 ?
  82. ?????????????*/??
  83. ????????????public?function?expendAllTreeNode():?void?{ ??
  84. ????????????????tree.validateNow(); ??
  85. ????????????????tree.selectedIndex?=?0; ??
  86. ????????????????tree.expandChildrenOf(tree.selectedItem,true); ??
  87. ????????????} ??
  88. ???????????? ??
  89. ????????????/* ?
  90. ????????????*?添加右键菜单 ?
  91. ????????????*/??
  92. ????????????public?function?addTreeMenu():void{ ??
  93. ????????????????var?menu:ContextMenu=new?ContextMenu(); ??
  94. ????????????????menu.hideBuiltInItems(); ??
  95. ????????????????menuItem=new?ContextMenuItem("打开"); ??
  96. ????????????????menuItem.visible=false; ??
  97. ????????????????menu.customItems.push(menuItem); ??
  98. ????????????????tree.contextMenu=menu; ??
  99. ????????????} ??
  100. ???????????? ??
  101. ????????????/* ?
  102. ????????????*?设置节点图标 ?
  103. ????????????*/??
  104. ????????????private?function?setIcon(item:Object):Class{ ??
  105. ????????????????var?node:XML?=?item?as?XML; ??
  106. ????????????????var?nodeIcon:String?=?node.@icon; ??
  107. ????????????????if(node!=null?&&?nodeIcon!=null?&&?nodeIcon.length!=0) ??
  108. ????????????????????return?iconArr[nodeIcon]; ??
  109. ????????????????else? ??
  110. ????????????????????return?null; ??
  111. ????????????} ??
  112. ???????????? ??
  113. ????????????/* ?
  114. ????????????*?处理右键事件 ?
  115. ????????????*/? ??
  116. ????????????private?function?rightClickHandler(ev:ContextMenuEvent):void{ ??
  117. ????????????????menuItem.visible=false; ??
  118. ????????????????var?tag:Object=ev.mouseTarget; ??
  119. ????????????????//右键时行选中,此处需捕获异常 ??
  120. ????????????????try{ ??
  121. ????????????????????if(tag?&&?tag.parent?&&?tag.parent.data?&&?(tag.parent.data?is?XML)) ??
  122. ????????????????????????tree.selectedItem?=?tag.parent.data; ??
  123. ????????????????}catch(ex:Error){ ??
  124. ????????????????} ??
  125. ???????????????? ??
  126. ????????????????var?node:XML?=?tree.selectedItem?as?XML; ??
  127. ????????????????if(node!=null?&&?node.@id!=null){ ??
  128. ????????????????????var?nodeId:String?=?node.@id; ??
  129. ????????????????????if(nodeId.length>0) ??
  130. ????????????????????????menuItem.visible?=?true; ??
  131. ????????????????} ??
  132. ????????????} ??
  133. ????????]]> ??
  134. ????</fx:Script> ??
  135. ????<s:HGroup?width="30%"?height="100%"> ??
  136. ????????<mx:Tree?id="tree"?width="100%"?height="100%"?dataProvider="{xml}"? ??
  137. ?????????????????labelField="@label"?iconFunction="setIcon"?/> ??
  138. ????</s:HGroup> ??
  139. </s:Application>??

?

?

图1:处理前效果

?

图2:处理后效果

------------------------------------------------------------------------------------------------------------

?

图3:处理前效果

?

?

图4:处理后效果

?

?

注意:运行环境 Flash Builder 4

  • 大小: 20.5 KB
  • 大小: 20.8 KB
  • 大小: 10.8 KB
  • 大小: 10.1 KB
  • TreeTest.rar (23 KB)
  • 下载次数: 93
  • 查看图片附件

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读