singlepath.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.min.js"></script>
  9. <!-- <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>-->
  10. <script type="text/javascript" src="../../../js/jquery.ztree.core.js"></script>
  11. <!-- <script type="text/javascript" src="../../../js/jquery.ztree.excheck.js"></script>
  12. <script type="text/javascript" src="../../../js/jquery.ztree.exedit.js"></script>-->
  13. <SCRIPT type="text/javascript">
  14. <!--
  15. var setting = {
  16. view: {
  17. dblClickExpand: false,
  18. showLine: false
  19. },
  20. data: {
  21. simpleData: {
  22. enable: true
  23. }
  24. },
  25. callback: {
  26. beforeExpand: beforeExpand,
  27. onExpand: onExpand,
  28. onClick: onClick
  29. }
  30. };
  31. function createNodes(maxNodesNumInLevel, maxLevel, curLevel, curPId) {
  32. if (maxNodesNumInLevel<5) {
  33. maxNodesNumInLevel = 5;
  34. }
  35. var nodes = [], num = 0;
  36. while(num<3) {
  37. num = parseInt(Math.random()*1024)%maxNodesNumInLevel+1;
  38. }
  39. for (var i=0; i<num; i++) {
  40. var id = curPId ? curPId + "-" + i : "" + i, isParent = (parseInt(Math.random()*9999)%3!=0),
  41. node = {id: id, pId : curPId, name : "N" + id};
  42. nodes.push(node);
  43. if (isParent && curLevel<maxLevel) {
  44. nodes = nodes.concat(createNodes(maxNodesNumInLevel, maxLevel, curLevel+1, id));
  45. }
  46. }
  47. return nodes;
  48. }
  49. var zNodes =createNodes(5, 5, 0);
  50. var curExpandNode = null;
  51. function beforeExpand(treeId, treeNode) {
  52. var pNode = curExpandNode ? curExpandNode.getParentNode():null;
  53. var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
  54. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  55. for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
  56. if (treeNode !== treeNodeP.children[i]) {
  57. zTree.expandNode(treeNodeP.children[i], false);
  58. }
  59. }
  60. while (pNode) {
  61. if (pNode === treeNode) {
  62. break;
  63. }
  64. pNode = pNode.getParentNode();
  65. }
  66. if (!pNode) {
  67. singlePath(treeNode);
  68. }
  69. }
  70. function singlePath(newNode) {
  71. if (newNode === curExpandNode) return;
  72. var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  73. rootNodes, tmpRoot, tmpTId, i, j, n;
  74. if (!curExpandNode) {
  75. tmpRoot = newNode;
  76. while (tmpRoot) {
  77. tmpTId = tmpRoot.tId;
  78. tmpRoot = tmpRoot.getParentNode();
  79. }
  80. rootNodes = zTree.getNodes();
  81. for (i=0, j=rootNodes.length; i<j; i++) {
  82. n = rootNodes[i];
  83. if (n.tId != tmpTId) {
  84. zTree.expandNode(n, false);
  85. }
  86. }
  87. } else if (curExpandNode && curExpandNode.open) {
  88. if (newNode.parentTId === curExpandNode.parentTId) {
  89. zTree.expandNode(curExpandNode, false);
  90. } else {
  91. var newParents = [];
  92. while (newNode) {
  93. newNode = newNode.getParentNode();
  94. if (newNode === curExpandNode) {
  95. newParents = null;
  96. break;
  97. } else if (newNode) {
  98. newParents.push(newNode);
  99. }
  100. }
  101. if (newParents!=null) {
  102. var oldNode = curExpandNode;
  103. var oldParents = [];
  104. while (oldNode) {
  105. oldNode = oldNode.getParentNode();
  106. if (oldNode) {
  107. oldParents.push(oldNode);
  108. }
  109. }
  110. if (newParents.length>0) {
  111. zTree.expandNode(oldParents[Math.abs(oldParents.length-newParents.length)-1], false);
  112. } else {
  113. zTree.expandNode(oldParents[oldParents.length-1], false);
  114. }
  115. }
  116. }
  117. }
  118. curExpandNode = newNode;
  119. }
  120. function onExpand(event, treeId, treeNode) {
  121. curExpandNode = treeNode;
  122. }
  123. function onClick(e,treeId, treeNode) {
  124. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  125. zTree.expandNode(treeNode, null, null, null, true);
  126. }
  127. $(document).ready(function(){
  128. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  129. });
  130. //-->
  131. </SCRIPT>
  132. <style type="text/css">
  133. .ztree li button.switch {visibility:hidden; width:1px;}
  134. .ztree li button.switch.roots_docu {visibility:visible; width:16px;}
  135. .ztree li button.switch.center_docu {visibility:visible; width:16px;}
  136. .ztree li button.switch.bottom_docu {visibility:visible; width:16px;}
  137. </style>
  138. </HEAD>
  139. <BODY>
  140. <h1>Keep Single Path</h1>
  141. <h6>[ File Path: super/singlepath.html ]</h6>
  142. <div class="content_wrap">
  143. <div class="zTreeDemoBackground left">
  144. <ul id="treeDemo" class="ztree"></ul>
  145. </div>
  146. <div class="right">
  147. <ul class="info">
  148. <li class="title"><h2>Explanation of implementation method</h2>
  149. <ul class="list">
  150. <li>This Demo is the transformation from "Click to Expand Node" demo​​, tree only expand single path.</li>
  151. <li class="highlight_red">Use 'setting.callback.beforeExpand / onExpand' callback function to achieve rules about expand </li>
  152. </ul>
  153. </li>
  154. </ul>
  155. </div>
  156. </div>
  157. </BODY>
  158. </HTML>