212ea2ac9571dafdb66f0fc751d2788290333a3a.svn-base 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>右键菜单</title>
  6. <!--框架必需start-->
  7. <script type="text/javascript" src="../../libs/js/jquery.js"></script>
  8. <script type="text/javascript" src="../../libs/js/language/cn.js"></script>
  9. <script type="text/javascript" src="../../libs/js/framework.js"></script>
  10. <link href="../../libs/css/import_basic.css" rel="stylesheet" type="text/css"/>
  11. <link rel="stylesheet" type="text/css" id="skin" prePath="../../"/>
  12. <link rel="stylesheet" type="text/css" id="customSkin"/>
  13. <!--框架必需end-->
  14. <!-- ztree start -->
  15. <script type="text/javascript" src="../../libs/js/tree/ztree/ztree.js"></script>
  16. <link href="../../libs/js/tree/ztree/ztree.css" rel="stylesheet" type="text/css"/>
  17. <!-- ztree end -->
  18. </head>
  19. <body>
  20. <div class="b-m-mpanel" style="width: 150px;visibility:hidden;" id="rMenu">
  21. <div class="b-m-item" id="m_add" onclick="addTreeNode();">
  22. <img align="middle" src="../../libs/images/icons/add.png"/><span>增加节点</span>
  23. </div>
  24. <div class="b-m-item" id="m_del" onclick="removeTreeNode();">
  25. <img align="middle" src="../../libs/images/icons/close.png"/><span>删除节点</span>
  26. </div>
  27. <div class="b-m-item" id="m_check" onclick="checkTreeNode(true);">
  28. <img align="middle" src="../../libs/images/icons/ico4.gif"/><span>勾选节点</span>
  29. </div>
  30. <div class="b-m-item" id="m_unCheck" onclick="checkTreeNode(false);">
  31. <img align="middle" src="../../libs/images/icons/ico4-3.gif"/><span>取消勾选节点</span>
  32. </div>
  33. <div class="b-m-item" id="m_reset" onclick="resetTree();">
  34. <img align="middle" src="../../libs/images/icons/ico4-1-1.gif"/><span>恢复</span>
  35. </div>
  36. </div>
  37. <div class="box1" panelWidth="800">
  38. <fieldset>
  39. <div>
  40. <ul id="tree-1" class="ztree"></ul>
  41. </div>
  42. </fieldset>
  43. </div>
  44. <script type="text/javascript">
  45. var zTree;
  46. var setting = {
  47. view: {
  48. dblClickExpand: false
  49. },
  50. check: {
  51. enable: true
  52. },
  53. callback: {
  54. //响应右键
  55. onRightClick: OnRightClick
  56. }
  57. };
  58. var zNodes = [
  59. {id:1, parentId:0, name:"无右键菜单", open:true, noR:true},
  60. {id:11, parentId:1, name:"无右键菜单", open:true, noR:true},
  61. {id:12, parentId:1, name:"无右键菜单", open:true, noR:true},
  62. {id:2, parentId:0, name:"右键菜单类型1", open:true, menuType:"nonLeafMenu"},
  63. {id:21, parentId:2, name:"右键菜单类型2", open:true, menuType:"leafMenu"},
  64. {id:22, parentId:2, name:"右键菜单类型2", open:true, menuType:"leafMenu"}
  65. ];
  66. function initComplete(){
  67. $.fn.zTree.init($("#tree-1"), setting, zNodes);
  68. zTree = $.fn.zTree.getZTreeObj("tree-1");
  69. //鼠标移入右键菜单效果
  70. $("#rMenu >div").hover(function(){
  71. $(this).addClass("b-m-ifocus");
  72. },function(){
  73. $(this).removeClass("b-m-ifocus");
  74. });
  75. }
  76. //点击右键处理
  77. function OnRightClick(event, treeId, treeNode) {
  78. //在节点之外显示一种菜单
  79. if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
  80. zTree.cancelSelectedNode();
  81. showRMenu("root", event.clientX, event.clientY);
  82. //在节点里面显示另一种菜单
  83. } else if(treeNode && !treeNode.noR) {
  84. zTree.selectNode(treeNode);
  85. showRMenu(treeNode.menuType, event.clientX, event.clientY);
  86. }
  87. }
  88. //根据设置弹出不同的右键菜单
  89. function showRMenu(type, x, y) {
  90. $("#rMenu ul").show();
  91. if(type == "root") {//树节点外只保留添加和恢复
  92. $("#m_add").show();
  93. $("#m_del").show();
  94. $("#m_check").show();
  95. $("#m_unCheck").show();
  96. $("#m_reset").show();
  97. $("#m_del").hide();
  98. $("#m_check").hide();
  99. $("#m_unCheck").hide();
  100. }else if(type == "nonLeafMenu") {//父节点保留添加和删除
  101. $("#m_add").show();
  102. $("#m_del").show();
  103. $("#m_check").show();
  104. $("#m_unCheck").show();
  105. $("#m_reset").show();
  106. $("#m_reset").hide();
  107. $("#m_check").hide();
  108. $("#m_unCheck").hide();
  109. $("#m_reset").hide();
  110. }else if(type == "leafMenu") {//子节点保留选择和删除
  111. $("#m_add").show();
  112. $("#m_del").show();
  113. $("#m_check").show();
  114. $("#m_unCheck").show();
  115. $("#m_reset").show();
  116. $("#m_add").hide();
  117. $("#m_reset").hide();
  118. }
  119. $("#rMenu").css({"top":y+"px", "left":x+"px", "visibility":"visible"});
  120. //点击菜单外时隐藏菜单
  121. $("body").bind("mousedown", onBodyMouseDown);
  122. }
  123. //点击菜单项时隐藏菜单
  124. function hideRMenu() {
  125. if ($("#rMenu")) $("#rMenu").css({"visibility": "hidden"});
  126. $("body").unbind("mousedown", onBodyMouseDown);
  127. }
  128. //点击菜单外时隐藏菜单
  129. function onBodyMouseDown(event){
  130. if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
  131. $("#rMenu").css({"visibility" : "hidden"});
  132. }
  133. }
  134. //以下是点击右键菜单的处理函数
  135. var addCount = 1;
  136. //添加节点
  137. function addTreeNode() {
  138. hideRMenu();
  139. zTree.addNodes(zTree.getSelectedNodes()[0], [{ name:"增加" + (addCount++),menuType:"type2"}]);
  140. }
  141. //确认是否删除+删除处理
  142. function removeTreeNode() {
  143. hideRMenu();
  144. var nodes = zTree.getSelectedNodes();
  145. if (nodes && nodes.length > 0) {
  146. if (nodes[0].children && nodes[0].children.length > 0) {
  147. var msg = "要删除的节点是父节点,如果删除将连同子节点一起删掉。\n\n请确认!";
  148. top.Dialog.confirm(msg, function(){
  149. zTree.removeNode(nodes[0]);
  150. //此处进行ajax后台数据处理
  151. });
  152. } else {
  153. top.Dialog.confirm("确认删除 节点 -- " + nodes[0].name + " 吗?",function(){
  154. zTree.removeNode(nodes[0]);
  155. //此处进行ajax后台数据处理
  156. });
  157. }
  158. }
  159. }
  160. // 勾选/取消勾选节点
  161. function checkTreeNode(checked) {
  162. var nodes = zTree.getSelectedNodes();
  163. if (nodes && nodes.length>0) {
  164. zTree.checkNode(nodes[0], checked, true);
  165. }
  166. hideRMenu();
  167. }
  168. //树的状态的重置
  169. function resetTree() {
  170. hideRMenu();
  171. $.fn.zTree.init($("#tree-1"), setting, zNodes);
  172. }
  173. </script>
  174. </body>
  175. </html>