singlepath.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!DOCTYPE html>
  2. <HTML>
  3. <HEAD>
  4. <TITLE> ZTREE DEMO - single path</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. showLine: false
  18. },
  19. data: {
  20. simpleData: {
  21. enable: true
  22. }
  23. },
  24. callback: {
  25. beforeExpand: beforeExpand,
  26. onExpand: onExpand,
  27. onClick: onClick
  28. }
  29. };
  30. function createNodes(maxNodesNumInLevel, maxLevel, curLevel, curPId) {
  31. if (maxNodesNumInLevel<5) {
  32. maxNodesNumInLevel = 5;
  33. }
  34. var nodes = [], num = 0;
  35. while(num<3) {
  36. num = parseInt(Math.random()*1024)%maxNodesNumInLevel+1;
  37. }
  38. for (var i=0; i<num; i++) {
  39. var id = curPId ? curPId + "-" + i : "" + i, isParent = (parseInt(Math.random()*9999)%3!=0),
  40. node = {id: id, pId : curPId, name : "N" + id};
  41. nodes.push(node);
  42. if (isParent && curLevel<maxLevel) {
  43. nodes = nodes.concat(createNodes(maxNodesNumInLevel, maxLevel, curLevel+1, id));
  44. }
  45. }
  46. return nodes;
  47. }
  48. var zNodes =createNodes(5, 5, 0);
  49. var curExpandNode = null;
  50. function beforeExpand(treeId, treeNode) {
  51. var pNode = curExpandNode ? curExpandNode.getParentNode():null;
  52. var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
  53. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  54. for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
  55. if (treeNode !== treeNodeP.children[i]) {
  56. zTree.expandNode(treeNodeP.children[i], false);
  57. }
  58. }
  59. while (pNode) {
  60. if (pNode === treeNode) {
  61. break;
  62. }
  63. pNode = pNode.getParentNode();
  64. }
  65. if (!pNode) {
  66. singlePath(treeNode);
  67. }
  68. }
  69. function singlePath(newNode) {
  70. if (newNode === curExpandNode) return;
  71. if (curExpandNode && curExpandNode.open==true) {
  72. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  73. if (newNode.parentTId === curExpandNode.parentTId) {
  74. zTree.expandNode(curExpandNode, false);
  75. } else {
  76. var newParents = [];
  77. while (newNode) {
  78. newNode = newNode.getParentNode();
  79. if (newNode === curExpandNode) {
  80. newParents = null;
  81. break;
  82. } else if (newNode) {
  83. newParents.push(newNode);
  84. }
  85. }
  86. if (newParents!=null) {
  87. var oldNode = curExpandNode;
  88. var oldParents = [];
  89. while (oldNode) {
  90. oldNode = oldNode.getParentNode();
  91. if (oldNode) {
  92. oldParents.push(oldNode);
  93. }
  94. }
  95. if (newParents.length>0) {
  96. zTree.expandNode(oldParents[Math.abs(oldParents.length-newParents.length)-1], false);
  97. } else {
  98. zTree.expandNode(oldParents[oldParents.length-1], false);
  99. }
  100. }
  101. }
  102. }
  103. curExpandNode = newNode;
  104. }
  105. function onExpand(event, treeId, treeNode) {
  106. curExpandNode = treeNode;
  107. }
  108. function onClick(e,treeId, treeNode) {
  109. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  110. zTree.expandNode(treeNode, null, null, null, true);
  111. }
  112. $(document).ready(function(){
  113. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  114. });
  115. //-->
  116. </SCRIPT>
  117. <style type="text/css">
  118. .ztree li button.switch {visibility:hidden; width:1px;}
  119. .ztree li button.switch.roots_docu {visibility:visible; width:16px;}
  120. .ztree li button.switch.center_docu {visibility:visible; width:16px;}
  121. .ztree li button.switch.bottom_docu {visibility:visible; width:16px;}
  122. </style>
  123. </HEAD>
  124. <BODY>
  125. <h1>保持展开单一路径</h1>
  126. <h6>[ 文件路径: super/singlepath.html ]</h6>
  127. <div class="content_wrap">
  128. <div class="zTreeDemoBackground left">
  129. <ul id="treeDemo" class="ztree"></ul>
  130. </div>
  131. <div class="right">
  132. <ul class="info">
  133. <li class="title"><h2>实现方法说明</h2>
  134. <ul class="list">
  135. <li>此 Demo 是在 "单击展开/折叠节点" 基础上改造而来,树节点保持始终只展开一条路径。</li>
  136. <li class="highlight_red">利用 setting.callback.beforeExpand / onExpand 事件回调函数实现展开规则</li>
  137. </ul>
  138. </li>
  139. </ul>
  140. </div>
  141. </div>
  142. </BODY>
  143. </HTML>