rightClickMenu.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <!DOCTYPE html>
  2. <HTML>
  3. <HEAD>
  4. <TITLE> ZTREE DEMO - select menu</TITLE>
  5. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6. <link rel="stylesheet" href="../../../css/demo.css" type="text/css">
  7. <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
  8. <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
  9. <script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
  10. <script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>
  11. <script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>
  12. <SCRIPT type="text/javascript">
  13. <!--
  14. var setting = {
  15. view: {
  16. dblClickExpand: false
  17. },
  18. check: {
  19. enable: true
  20. },
  21. callback: {
  22. onRightClick: OnRightClick
  23. }
  24. };
  25. var zNodes =[
  26. {id:1, name:"无右键菜单 1", open:true, noR:true,
  27. children:[
  28. {id:11, name:"节点 1-1", noR:true},
  29. {id:12, name:"节点 1-2", noR:true}
  30. ]},
  31. {id:2, name:"右键操作 2", open:true,
  32. children:[
  33. {id:21, name:"节点 2-1"},
  34. {id:22, name:"节点 2-2"},
  35. {id:23, name:"节点 2-3"},
  36. {id:24, name:"节点 2-4"}
  37. ]},
  38. {id:3, name:"右键操作 3", open:true,
  39. children:[
  40. {id:31, name:"节点 3-1"},
  41. {id:32, name:"节点 3-2"},
  42. {id:33, name:"节点 3-3"},
  43. {id:34, name:"节点 3-4"}
  44. ]}
  45. ];
  46. function OnRightClick(event, treeId, treeNode) {
  47. if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
  48. zTree.cancelSelectedNode();
  49. showRMenu("root", event.clientX, event.clientY);
  50. } else if (treeNode && !treeNode.noR) {
  51. zTree.selectNode(treeNode);
  52. showRMenu("node", event.clientX, event.clientY);
  53. }
  54. }
  55. function showRMenu(type, x, y) {
  56. $("#rMenu ul").show();
  57. if (type=="root") {
  58. $("#m_del").hide();
  59. $("#m_check").hide();
  60. $("#m_unCheck").hide();
  61. } else {
  62. $("#m_del").show();
  63. $("#m_check").show();
  64. $("#m_unCheck").show();
  65. }
  66. rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});
  67. $("body").bind("mousedown", onBodyMouseDown);
  68. }
  69. function hideRMenu() {
  70. if (rMenu) rMenu.css({"visibility": "hidden"});
  71. $("body").unbind("mousedown", onBodyMouseDown);
  72. }
  73. function onBodyMouseDown(event){
  74. if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
  75. rMenu.css({"visibility" : "hidden"});
  76. }
  77. }
  78. var addCount = 1;
  79. function addTreeNode() {
  80. hideRMenu();
  81. var newNode = { name:"增加" + (addCount++)};
  82. if (zTree.getSelectedNodes()[0]) {
  83. newNode.checked = zTree.getSelectedNodes()[0].checked;
  84. zTree.addNodes(zTree.getSelectedNodes()[0], newNode);
  85. } else {
  86. zTree.addNodes(null, newNode);
  87. }
  88. }
  89. function removeTreeNode() {
  90. hideRMenu();
  91. var nodes = zTree.getSelectedNodes();
  92. if (nodes && nodes.length>0) {
  93. if (nodes[0].children && nodes[0].children.length > 0) {
  94. var msg = "要删除的节点是父节点,如果删除将连同子节点一起删掉。\n\n请确认!";
  95. if (confirm(msg)==true){
  96. zTree.removeNode(nodes[0]);
  97. }
  98. } else {
  99. zTree.removeNode(nodes[0]);
  100. }
  101. }
  102. }
  103. function checkTreeNode(checked) {
  104. var nodes = zTree.getSelectedNodes();
  105. if (nodes && nodes.length>0) {
  106. zTree.checkNode(nodes[0], checked, true);
  107. }
  108. hideRMenu();
  109. }
  110. function resetTree() {
  111. hideRMenu();
  112. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  113. }
  114. var zTree, rMenu;
  115. $(document).ready(function(){
  116. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  117. zTree = $.fn.zTree.getZTreeObj("treeDemo");
  118. rMenu = $("#rMenu");
  119. });
  120. //-->
  121. </SCRIPT>
  122. <style type="text/css">
  123. div#rMenu {position:absolute; visibility:hidden; top:0; background-color: #555;text-align: left;padding: 2px;}
  124. div#rMenu ul li{
  125. margin: 1px 0;
  126. padding: 0 5px;
  127. cursor: pointer;
  128. list-style: none outside none;
  129. background-color: #DFDFDF;
  130. }
  131. </style>
  132. </HEAD>
  133. <BODY>
  134. <h1>右键菜单的实现</h1>
  135. <h6>[ 文件路径: super/rightClickMenu.html ]</h6>
  136. <div class="content_wrap">
  137. <div class="zTreeDemoBackground left">
  138. <ul id="treeDemo" class="ztree"></ul>
  139. </div>
  140. <div class="right">
  141. <ul class="info">
  142. <li class="title"><h2>实现方法说明</h2>
  143. <ul class="list">
  144. <li>利用 beforeRightClick / onRightClick 事件回调函数简单实现的右键菜单</li>
  145. <li class="highlight_red">Demo 中的菜单比较简陋,你完全可以配合其他自定义样式的菜单图层混合使用</li>
  146. </ul>
  147. </li>
  148. </ul>
  149. </div>
  150. </div>
  151. <div id="rMenu">
  152. <ul>
  153. <li id="m_add" onclick="addTreeNode();">增加节点</li>
  154. <li id="m_del" onclick="removeTreeNode();">删除节点</li>
  155. <li id="m_check" onclick="checkTreeNode(true);">Check节点</li>
  156. <li id="m_unCheck" onclick="checkTreeNode(false);">unCheck节点</li>
  157. <li id="m_reset" onclick="resetTree();">恢复zTree</li>
  158. </ul>
  159. </div>
  160. </BODY>
  161. </HTML>