diy_async.html 5.5 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个节点", id:"1", count:500, times:1, isParent:true},
  38. {name:"1000个节点", id:"2", count:1000, times:1, isParent:true},
  39. {name:"2000个节点", 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("zTree 正在下载数据中,请稍后展开节点。。。");
  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("加载完毕,共进行 "+ (treeNode.times-1) +" 次异步加载, 耗时:"+ usedTime + " 秒");
  77. }
  78. }
  79. function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
  80. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  81. alert("异步获取数据出现异常。");
  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>分批异步加载大数据量</h1>
  116. <h6>[ 文件路径: 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 专门用于测试分批异步加载,每次展开节点都要重新进行异步加载。</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、大数据量加载说明</h2>
  127. <ul class="list">
  128. <li>1)、对于某一级节点数多达几千个的时候,zTree 默认的延迟加载是无效的,此 Demo 演示了一种原先 zTree v2.6 时的分批加载节点的方法。</li>
  129. <li class="highlight_red">2)、此方法适用于1、2千个节点必须全部显示的需求。</li>
  130. <li class="highlight_red">3)、此方法并不能解决加载慢的问题,相反只会让最终结果出现的更慢,只是可以有限度的避免浏览器假死,而且显示的节点越多就越慢。</li>
  131. <li>4)、对于某一级节点数至少几千个的情况,另一个解决方案是:分页异步加载。<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、setting 配置信息说明</h2>
  137. <ul class="list">
  138. <li>需要设置 setting.async 异步加载部分的参数</li>
  139. <li>建议关闭动画效果 setting.view.expandSpeed = "";</li>
  140. <li>其他不需要进行特殊的配置,根据自己的需求自行设置</li>
  141. </ul>
  142. </li>
  143. <li class="title"><h2>3、treeNode 节点数据说明</h2>
  144. <ul class="list">
  145. <li>对 节点数据 没有特殊要求,用户可以根据自己的需求添加自定义属性</li>
  146. </ul>
  147. </li>
  148. </ul>
  149. </div>
  150. </div>
  151. </BODY>
  152. </HTML>