diy_async.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <!DOCTYPE html>
  2. <HTML>
  3. <HEAD>
  4. <TITLE> ZTREE DEMO - big data async</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. async: {
  16. enable: true,
  17. url: getUrl
  18. },
  19. check: {
  20. enable: true
  21. },
  22. data: {
  23. simpleData: {
  24. enable: true
  25. }
  26. },
  27. view: {
  28. expandSpeed: ""
  29. },
  30. callback: {
  31. beforeExpand: beforeExpand,
  32. onAsyncSuccess: onAsyncSuccess,
  33. onAsyncError: onAsyncError
  34. }
  35. };
  36. var zNodes =[
  37. {name:"500 nodes", id:"1", count:500, times:1, isParent:true},
  38. {name:"1000 nodes", id:"2", count:1000, times:1, isParent:true},
  39. {name:"2000 nodes", id:"3", count:2000, times:1, isParent:true}
  40. ];
  41. var log, className = "dark",
  42. startTime = 0, endTime = 0, perCount = 100, perTime = 100;
  43. function getUrl(treeId, treeNode) {
  44. var curCount = (treeNode.children) ? treeNode.children.length : 0;
  45. var getCount = (curCount + perCount) > treeNode.count ? (treeNode.count - curCount) : perCount;
  46. var param = "id="+treeNode.id+"_"+(treeNode.times++) +"&count="+getCount;
  47. return "../asyncData/getNodesForBigData.php?" + param;
  48. }
  49. function beforeExpand(treeId, treeNode) {
  50. if (!treeNode.isAjaxing) {
  51. startTime = new Date();
  52. treeNode.times = 1;
  53. ajaxGetNodes(treeNode, "refresh");
  54. return true;
  55. } else {
  56. alert("Downloading data, Please wait to expand node...");
  57. return false;
  58. }
  59. }
  60. function onAsyncSuccess(event, treeId, treeNode, msg) {
  61. if (!msg || msg.length == 0) {
  62. return;
  63. }
  64. var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  65. totalCount = treeNode.count;
  66. if (treeNode.children.length < totalCount) {
  67. setTimeout(function() {ajaxGetNodes(treeNode);}, perTime);
  68. } else {
  69. treeNode.icon = "";
  70. zTree.updateNode(treeNode);
  71. zTree.selectNode(treeNode.children[0]);
  72. endTime = new Date();
  73. var usedTime = (endTime.getTime() - startTime.getTime())/1000;
  74. className = (className === "dark" ? "":"dark");
  75. showLog("[ "+getTime()+" ]&nbsp;&nbsp;treeNode:" + treeNode.name );
  76. showLog("Child node has finished loading, a total of "+ (treeNode.times-1) +" times the asynchronous load, elapsed time: "+ usedTime + " seconds ");
  77. }
  78. }
  79. function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
  80. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  81. alert("ajax error...");
  82. treeNode.icon = "";
  83. zTree.updateNode(treeNode);
  84. }
  85. function ajaxGetNodes(treeNode, reloadType) {
  86. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  87. if (reloadType == "refresh") {
  88. treeNode.icon = "../../../css/zTreeStyle/img/loading.gif";
  89. zTree.updateNode(treeNode);
  90. }
  91. zTree.reAsyncChildNodes(treeNode, reloadType, true);
  92. }
  93. function showLog(str) {
  94. if (!log) log = $("#log");
  95. log.append("<li class='"+className+"'>"+str+"</li>");
  96. if(log.children("li").length > 4) {
  97. log.get(0).removeChild(log.children("li")[0]);
  98. }
  99. }
  100. function getTime() {
  101. var now= new Date(),
  102. h=now.getHours(),
  103. m=now.getMinutes(),
  104. s=now.getSeconds(),
  105. ms=now.getMilliseconds();
  106. return (h+":"+m+":"+s+ " " +ms);
  107. }
  108. $(document).ready(function(){
  109. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  110. });
  111. //-->
  112. </SCRIPT>
  113. </HEAD>
  114. <BODY>
  115. <h1>Loading Data in Batches</h1>
  116. <h6>[ File Path: bigdata/diy_async.html ]</h6>
  117. <div class="content_wrap">
  118. <div class="zTreeDemoBackground left">
  119. <ul>
  120. <li class="highlight_red">&nbsp;&nbsp;&nbsp;&nbsp;Demo for testing load data in batches, each node needs to re-start to load.</li>
  121. </ul>
  122. <ul id="treeDemo" class="ztree"></ul>
  123. </div>
  124. <div class="right">
  125. <ul class="info">
  126. <li class="title"><h2>1, Explanation of large data load</h2>
  127. <ul class="list">
  128. <li>1) If has nodes for as many as thousands in one level, lazy loading is invalid, this demo shows how to load data in batches.</li>
  129. <li class="highlight_red">2) This method applies to thousands of nodes must all display needs.</li>
  130. <li class="highlight_red">3) This method doesn't solve the problem of slow loading, it will only make the final result appear more slowly, but can be limited to avoid browser suspended animation, and more nodes displayed slower.</li>
  131. <li>4) For at least several thousand nodes in one level case, another solution is to: pagination loading.<br/>
  132. async load log:<br/>
  133. <ul id="log" class="log" style="height:85px"></ul></li>
  134. </ul>
  135. </li>
  136. <li class="title"><h2>2, Explanation of setting</h2>
  137. <ul class="list">
  138. <li>Need to set the parameters in setting.async</li>
  139. <li>Advised to turn off animation effects: setting.view.expandSpeed = "";</li>
  140. <li>No other special configuration, the user can set their own requirements.</li>
  141. </ul>
  142. </li>
  143. <li class="title"><h2>3, Explanation of treeNode</h2>
  144. <ul class="list">
  145. <li>No special requirements on the node data, the user can add custom attributes.</li>
  146. </ul>
  147. </li>
  148. </ul>
  149. </div>
  150. </div>
  151. </BODY>
  152. </HTML>