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

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

发布时间:2020-12-15 04:05:15 所属栏目:百科 来源:网络整理
导读:转自:http://www.voidcn.com/article/p-xuxruvom-na.html 直接上代码: [html] ? view plain copy print ? ? xml ? version = "1.0" ? encoding = "utf-8" ? ?? s:Application ? xmlns:fx = "http://ns.adobe.com/mxml/2009" ?? ??????????????? xmlns:s =

转自:http://www.voidcn.com/article/p-xuxruvom-na.html

直接上代码:

[html]? view plain copy print ?
  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"?minWidth="955"?minHeight="600"??
  5. ???????????????>??
  6. ????<fx:Script>??
  7. ????????<![CDATA[?
  8. ????????????import?mx.collections.XMLListCollection;?
  9. ????????????import?mx.controls.Alert;?
  10. ?????????????
  11. ????????????[Bindable]?
  12. ????????????[Embed(source="images/open.png")]?
  13. ????????????public?var?folderOpen:Class;?
  14. ????????????[Embed(source="images/close.png")]?
  15. ????????????public?var?folderClose:Class;?
  16. ????????????[Embed(source="images/file.png")]?
  17. ????????????public?var?fileInfo:Class;?
  18. ????????????private?var?company:XML=?
  19. ????????????<department>?
  20. ????????????????<department?name="部门A">?
  21. ????????????????????<department?name="小A"?/>?
  22. ????????????????????<department?name="小B"?/>?
  23. ????????????????</department>?
  24. ?????????????????
  25. ????????????????<department?name="部门B">?
  26. ????????????????????<department?name="小C">?
  27. ????????????????????????<department?name="abc">?
  28. ????????????????????????????<department?name="efg"/>?
  29. ????????????????????????</department>?
  30. ????????????????????????<department?name="opqr"/>?
  31. ????????????????????????<department?name="小D">?
  32. ????????????????????????????<department?name="ff"/>?
  33. ????????????????????</department>?
  34. ????????????</department>;?
  35. ?????????????
  36. ????????????[Bindable]?
  37. ????????????private?var?companyData:XMLListCollection=new?XMLListCollection(company.department);?
  38. ????????????private?function?addNode():void?
  39. ????????????{?
  40. ????????????????//?新建节点?
  41. ????????????????var?newNode:XML=<employee/>;?
  42. ????????????????newNode.@name=empName.text;?
  43. ????????????????//?添加节点?
  44. ????????????????var?xml:XML=XML(tree1.selectedItem);?
  45. ????????????????if?(xml.length()?>?0)?
  46. ????????????????{?
  47. ????????????????????xml[0].appendChild(newNode);?
  48. ????????????????????tree1.expandChildrenOf(tree1.selectedItem,true);?
  49. ????????????????}?
  50. ????????????}?
  51. ????????????private?function?removeNode():void?
  52. ????????????????if?(tree1.selectedItem?!=?null)?
  53. ????????????????{?
  54. ????????????????????var?node:XML=XML(tree1.selectedItem);?
  55. ????????????????????Alert.show("根节点不能删除!","[错误]");?
  56. ????????????????????//if(tree.dataDescriptor.root){?
  57. ????????????????????????///Alert.show("根节点不能删除!",147); background-color:inherit">????????????????????????//return;?
  58. ????????????????????//}?
  59. ????????????????????var?nodeP:XML=node.parent();?
  60. ????????????????????var?childrenList:XMLListCollection=new?XMLListCollection(XMLList(nodeP).children());?
  61. ????????????????????var?i:Number=childrenList.getItemIndex(node);?
  62. ????????????????????childrenList.removeItemAt(i);?
  63. ????????????????????if(childrenList.length?==?0)?
  64. ????????????????????????tree1.selectedItem?=?nodeP;?
  65. ????????????????????else{?
  66. ????????????????????????if(i?==?childrenList.length)?
  67. ????????????????????????????i?=?i?-?1;?
  68. ????????????????????????tree1.selectedItem?=?childrenList.getItemAt(i)?
  69. ????????????????????}?
  70. ?????????????????????
  71. ????????????????}else{?
  72. ????????????????????Alert.show("请选中一个节点再进行删除!");?
  73. ????????????????}?
  74. ????????????}?
  75. ????????????/*?
  76. ?????????????*?根据节点名字模糊查询?
  77. ????????????*/?
  78. ????????????private?function?findNode(key:String):void?{?
  79. ????????????????//descendants()返回除了根节点以外的所有节点,然后根据条件筛选符合条件的结果集?
  80. ????????????????var?list:XMLList??=?company.descendants().(@name.indexOf(key)?!=?-1);?
  81. ????????????????expandParents(list[0]);?
  82. ????????????????tree1.selectedItem?=?list[0];?
  83. ????????????/*?
  84. ?????????????*?展开?
  85. ????????????*/?
  86. ????????????private?function?expandParents(xmlNode:XML):void?{?
  87. ????????????????while?(xmlNode.parent()?!=?null)?{???
  88. ????????????????????xmlNode?=?xmlNode.parent();?
  89. ????????????????????tree1.expandItem(xmlNode,true,?false);?
  90. ????????????//展开所有?
  91. ????????????private?function?expandAll():void?{?
  92. ????????????????tree1.expandChildrenOf(tree1.dataProvider[0],147); background-color:inherit">????????????//收起所有?
  93. ????????????private?function?closeAll():void?{?
  94. ????????]]>??
  95. ????</fx:Script>??
  96. ????<s:layout>??
  97. ????????<s:HorizontalLayout?horizontalAlign="center"?verticalAlign="middle"/>??
  98. ????</s:layout>??
  99. ????<s:Panel?title="实现添加和删除节点"??
  100. ??????????????width="450"??
  101. ??????????????height="450"??
  102. ??????????????>??
  103. ????<s:layout>??
  104. ????????<s:HorizontalLayout/>??
  105. ????</s:layout>??
  106. ????????<mx:Tree?id="tree1"???
  107. ?????????????????dataProvider="{company}"???
  108. ?????????????????labelField="@name"??
  109. ?????????????????height="300"?showRoot="false"??
  110. ?????????????????width="260"??
  111. ?????????????????defaultLeafIcon="{fileInfo}"??
  112. ?????????????????folderOpenIcon="{folderOpen}"??
  113. ?????????????????folderClosedIcon="{folderClose}"??
  114. ?????????????????/>??
  115. ????????<mx:VBox>??
  116. ????????????<s:Button?label="展开所有"?click="expandAll()"/>??
  117. ????????????<s:Button?label="关闭所有"?click="closeAll()"/>??
  118. ????????????<mx:Button?label="删除节点"?click="removeNode();"/>??
  119. ????????????<mx:HBox>??
  120. ????????????????<s:TextInput?id="empName"??
  121. ??????????????????????????????width="60"??
  122. ??????????????????????????????click="{empName.text?=?''}"??
  123. ??????????????????????????????prompt="新节点名"??
  124. ??????????????????????????????/>??
  125. ????????????????<mx:Button?label="添加节点"??
  126. ???????????????????????????click="addNode();"/>??
  127. ????????????</mx:HBox>??????
  128. ????????????<mx:HBox>??
  129. ????????????????<s:TextInput?id="keyName"??
  130. ??????????????????????????????width="60"??
  131. ??????????????????????????????prompt="关键字"/>??
  132. ????????????????<mx:Button?label="查找节点"??
  133. ???????????????????????????click="findNode(keyName.text)"/>??
  134. ????????????</mx:HBox>??
  135. ????????</mx:VBox>??
  136. ????</s:Panel>??
  137. </s:Application>??
里面有设置展开和关闭和文件的图片

(编辑:李大同)

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

    推荐文章
      热点阅读