7b0036f148f9fc1e5b5762bbc3b6893c4d491939.svn-base 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>异步加载数据</title>
  6. <!--框架必需start-->
  7. <script type="text/javascript" src="../../libs/js/jquery.js"></script>
  8. <script type="text/javascript" src="../../libs/js/language/cn.js"></script>
  9. <script type="text/javascript" src="../../libs/js/framework.js"></script>
  10. <link href="../../libs/css/import_basic.css" rel="stylesheet" type="text/css"/>
  11. <link rel="stylesheet" type="text/css" id="skin" prePath="../../"/>
  12. <link rel="stylesheet" type="text/css" id="customSkin"/>
  13. <!--框架必需end-->
  14. <!-- 树组件start -->
  15. <script type="text/javascript" src="../../libs/js/tree/ztree/ztree.js"></script>
  16. <link href="../../libs/js/tree/ztree/ztree.css" rel="stylesheet" type="text/css"/>
  17. <!-- 树组件end -->
  18. </head>
  19. <body>
  20. <div class="box1" panelWidth="800">
  21. <fieldset>
  22. <legend>1、基本使用</legend>
  23. <div class="red">此示例由后台支持</div>
  24. <p>可以返回单根树或多根树</p>
  25. <div>
  26. <ul id="tree-1" class="ztree"></ul>
  27. </div>
  28. </fieldset>
  29. <div class="height_15"></div>
  30. <fieldset>
  31. <legend>2、事件控制</legend>
  32. <div class="red">此示例由后台支持</div>
  33. <span>事件日志</span>
  34. <textarea id="log2" resize="true" readonly="readonly" style="height:170px; width:300px;"></textarea>
  35. <input type="button" value="清除日志" onclick="$('#log2').val('')"/>
  36. <div>
  37. <ul id="tree-2" class="ztree"></ul>
  38. </div>
  39. </fieldset>
  40. <div class="height_15"></div>
  41. <fieldset>
  42. <legend>3、全部展开</legend>
  43. <div class="red">此示例由后台支持</div>
  44. <p>可以返回单根树或多根树</p>
  45. <div>
  46. [ <a id="expandAllBtn" onclick="expandAll()">全部展开</a> ] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  47. [ <a id="asyncAllBtn" onclick="asyncAll()">后台自动全部加载</a> ]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  48. [ <a id="resetBtn" onclick="reset()">重置树</a> ]<br/><br/>
  49. <p class="highlight_red" id="demoMsg"></p>
  50. <ul id="tree-3" class="ztree"></ul>
  51. </div>
  52. </fieldset>
  53. <div class="height_15"></div>
  54. </div>
  55. <script type="text/javascript">
  56. //示例1相关
  57. var setting1 = {
  58. async: {
  59. enable: true,
  60. dataType: 'JSON',
  61. //返回的JSON数据的名字
  62. dataName: 'treeNodes',
  63. url: "../../organizationAction.do?method=async",
  64. autoParam: ["id", "name", "name=reName"],
  65. otherParam: {
  66. "otherParam1": "zTreeAsyncTest1",
  67. "otherParam2": "zTreeAsyncTest2"
  68. },
  69. //传回的数据格式不是ztree所需要的格式的时候,可以对数据进行转化。正常情况下不需要实现dataFilter
  70. dataFilter: filter
  71. }
  72. };
  73. //示例2相关
  74. var setting2 = {
  75. async: {
  76. enable: true,
  77. dataType: 'JSON',
  78. //返回的JSON数据的名字
  79. dataName: 'treeNodes',
  80. url: "../../organizationAction.do?method=async",
  81. autoParam: ["id"]
  82. },
  83. callback: {
  84. beforeAsync: beforeAsync2,
  85. onAsyncError: onAsyncError2,
  86. onAsyncSuccess: onAsyncSuccess2
  87. }
  88. };
  89. //示例3相关
  90. var demoMsg = {
  91. async:"正在进行异步加载,请等一会儿再点击...",
  92. expandAllOver: "全部展开完毕",
  93. asyncAllOver: "后台异步加载完毕",
  94. asyncAll: "已经异步加载完毕,不再重新加载",
  95. expandAll: "已经异步加载完毕,使用 expandAll 方法"
  96. }
  97. var setting3 = {
  98. async: {
  99. enable: true,
  100. dataType: 'JSON',
  101. //返回的JSON数据的名字
  102. dataName: 'treeNodes',
  103. url: "../../organizationAction.do?method=async",
  104. autoParam: ["id", "name", "name=reName"]
  105. },
  106. callback: {
  107. beforeAsync: beforeAsync,
  108. onAsyncSuccess: onAsyncSuccess,
  109. onAsyncError: onAsyncError
  110. }
  111. };
  112. function initComplete(){
  113. /*
  114. $.fn.zTree.init($("#tree-1"), setting1);
  115. $.fn.zTree.init($("#tree-2"), setting2);
  116. $.fn.zTree.init($("#tree-3"), setting3);
  117. */
  118. }
  119. //因为返回的数据格式正确,因此,直接返回。正常使用时是不需要此方法的。
  120. function filter(treeId, parentNode, childNodes) {
  121. return childNodes;
  122. }
  123. //示例2相关
  124. function beforeAsync2(treeId, treeNode){
  125. if(undefined != treeNode){
  126. appendToLog2("beforeAsync", treeNode.id, treeNode.name);
  127. }else{
  128. appendToLog2("beforeAsync", '', 'Root');
  129. }
  130. }
  131. function onAsyncError2(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown){
  132. if(undefined != treeNode){
  133. appendToLog2("onAsyncError", treeNode.id, treeNode.name);
  134. }else{
  135. appendToLog2("onAsyncError", '', 'Root');
  136. }
  137. }
  138. function onAsyncSuccess2(event, treeId, treeNode, msg){
  139. if(undefined != treeNode){
  140. appendToLog2("onAsyncSuccess", treeNode.id, treeNode.name);
  141. }else{
  142. appendToLog2("onAsyncSuccess", '', 'Root');
  143. }
  144. }
  145. function appendToLog2(eventName, nodeId, nodeName){
  146. $('#log2').val( function(index, val){
  147. return val + "【" + eventName + "】 " + nodeId + " " + nodeName + "\n";
  148. });
  149. }
  150. //示例3相关
  151. function beforeAsync() {
  152. curAsyncCount++;
  153. }
  154. function onAsyncSuccess(event, treeId, treeNode, msg) {
  155. curAsyncCount--;
  156. if (curStatus == "expand") {
  157. expandNodes(treeNode.children);
  158. } else if (curStatus == "async") {
  159. asyncNodes(treeNode.children);
  160. }
  161. if (curAsyncCount <= 0) {
  162. if (curStatus != "init" && curStatus != "") {
  163. $("#demoMsg").text((curStatus == "expand") ? demoMsg.expandAllOver : demoMsg.asyncAllOver);
  164. asyncForAll = true;
  165. }
  166. curStatus = "";
  167. }
  168. }
  169. function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
  170. curAsyncCount--;
  171. if (curAsyncCount <= 0) {
  172. curStatus = "";
  173. if (treeNode!=null) asyncForAll = true;
  174. }
  175. }
  176. var curStatus = "init", curAsyncCount = 0, asyncForAll = false,
  177. goAsync = false;
  178. function expandAll() {
  179. if (!check()) {
  180. return;
  181. }
  182. var zTree = $.fn.zTree.getZTreeObj("tree-3");
  183. if (asyncForAll) {
  184. $("#demoMsg").text(demoMsg.expandAll);
  185. zTree.expandAll(true);
  186. } else {
  187. expandNodes(zTree.getNodes());
  188. if (!goAsync) {
  189. $("#demoMsg").text(demoMsg.expandAll);
  190. curStatus = "";
  191. }
  192. }
  193. }
  194. function expandNodes(nodes) {
  195. if (!nodes) return;
  196. curStatus = "expand";
  197. var zTree = $.fn.zTree.getZTreeObj("tree-3");
  198. for (var i=0, l=nodes.length; i<l; i++) {
  199. zTree.expandNode(nodes[i], true, false, false);
  200. if (nodes[i].isParent && nodes[i].zAsync) {
  201. expandNodes(nodes[i].children);
  202. } else {
  203. goAsync = true;
  204. }
  205. }
  206. }
  207. function asyncAll() {
  208. if (!check()) {
  209. return;
  210. }
  211. var zTree = $.fn.zTree.getZTreeObj("tree-3");
  212. if (asyncForAll) {
  213. $("#demoMsg").text(demoMsg.asyncAll);
  214. } else {
  215. asyncNodes(zTree.getNodes());
  216. if (!goAsync) {
  217. $("#demoMsg").text(demoMsg.asyncAll);
  218. curStatus = "";
  219. }
  220. }
  221. }
  222. function asyncNodes(nodes) {
  223. if (!nodes) return;
  224. curStatus = "async";
  225. var zTree = $.fn.zTree.getZTreeObj("tree-3");
  226. for (var i=0, l=nodes.length; i<l; i++) {
  227. if (nodes[i].isParent && nodes[i].zAsync) {
  228. asyncNodes(nodes[i].children);
  229. } else {
  230. goAsync = true;
  231. zTree.reAsyncChildNodes(nodes[i], "refresh", true);
  232. }
  233. }
  234. }
  235. function reset() {
  236. if (!check()) {
  237. return;
  238. }
  239. asyncForAll = false;
  240. goAsync = false;
  241. $("#demoMsg").text("");
  242. $.fn.zTree.init($("#tree-3"), setting3);
  243. }
  244. function check() {
  245. if (curAsyncCount > 0) {
  246. $("#demoMsg").text(demoMsg.async);
  247. return false;
  248. }
  249. return true;
  250. }
  251. </script>
  252. </body>
  253. </html>