singlepath.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.js"></script>
  10. <!-- <script type="text/javascript" src="../../../js/jquery.ztree.excheck.js"></script>
  11. <script type="text/javascript" src="../../../js/jquery.ztree.exedit.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. var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  72. rootNodes, tmpRoot, tmpTId, i, j, n;
  73. if (!curExpandNode) {
  74. tmpRoot = newNode;
  75. while (tmpRoot) {
  76. tmpTId = tmpRoot.tId;
  77. tmpRoot = tmpRoot.getParentNode();
  78. }
  79. rootNodes = zTree.getNodes();
  80. for (i=0, j=rootNodes.length; i<j; i++) {
  81. n = rootNodes[i];
  82. if (n.tId != tmpTId) {
  83. zTree.expandNode(n, false);
  84. }
  85. }
  86. } else if (curExpandNode && curExpandNode.open) {
  87. if (newNode.parentTId === curExpandNode.parentTId) {
  88. zTree.expandNode(curExpandNode, false);
  89. } else {
  90. var newParents = [];
  91. while (newNode) {
  92. newNode = newNode.getParentNode();
  93. if (newNode === curExpandNode) {
  94. newParents = null;
  95. break;
  96. } else if (newNode) {
  97. newParents.push(newNode);
  98. }
  99. }
  100. if (newParents!=null) {
  101. var oldNode = curExpandNode;
  102. var oldParents = [];
  103. while (oldNode) {
  104. oldNode = oldNode.getParentNode();
  105. if (oldNode) {
  106. oldParents.push(oldNode);
  107. }
  108. }
  109. if (newParents.length>0) {
  110. zTree.expandNode(oldParents[Math.abs(oldParents.length-newParents.length)-1], false);
  111. } else {
  112. zTree.expandNode(oldParents[oldParents.length-1], false);
  113. }
  114. }
  115. }
  116. }
  117. curExpandNode = newNode;
  118. }
  119. function onExpand(event, treeId, treeNode) {
  120. curExpandNode = treeNode;
  121. }
  122. function onClick(e,treeId, treeNode) {
  123. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  124. zTree.expandNode(treeNode, null, null, null, true);
  125. }
  126. $(document).ready(function(){
  127. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  128. });
  129. //-->
  130. </SCRIPT>
  131. <style type="text/css">
  132. .ztree li button.switch {visibility:hidden; width:1px;}
  133. .ztree li button.switch.roots_docu {visibility:visible; width:16px;}
  134. .ztree li button.switch.center_docu {visibility:visible; width:16px;}
  135. .ztree li button.switch.bottom_docu {visibility:visible; width:16px;}
  136. </style>
  137. </HEAD>
  138. <BODY>
  139. <h1>Keep Single Path</h1>
  140. <h6>[ File Path: super/singlepath.html ]</h6>
  141. <div class="content_wrap">
  142. <div class="zTreeDemoBackground left">
  143. <ul id="treeDemo" class="ztree"></ul>
  144. </div>
  145. <div class="right">
  146. <ul class="info">
  147. <li class="title"><h2>Explanation of implementation method</h2>
  148. <ul class="list">
  149. <li>This Demo is the transformation from "Click to Expand Node" demo​​, tree only expand single path.</li>
  150. <li class="highlight_red">Use 'setting.callback.beforeExpand / onExpand' callback function to achieve rules about expand </li>
  151. </ul>
  152. </li>
  153. </ul>
  154. </div>
  155. </div>
  156. </BODY>
  157. </HTML>