lazyload.html 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Lazy Load Tree Nodes - jQuery EasyUI Demo</title>
  6. <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
  7. <link rel="stylesheet" type="text/css" href="../../themes/icon.css">
  8. <link rel="stylesheet" type="text/css" href="../demo.css">
  9. <script type="text/javascript" src="../../jquery.min.js"></script>
  10. <script type="text/javascript" src="../../jquery.easyui.min.js"></script>
  11. </head>
  12. <body>
  13. <h2>Lazy Load Tree Nodes</h2>
  14. <p>Get full hierarchical tree data but lazy load nodes level by level.</p>
  15. <div style="margin:20px 0;"></div>
  16. <div class="easyui-panel" style="padding:5px">
  17. <ul class="easyui-tree" data-options="url:'tree_data1.json',method:'get',loadFilter:myLoadFilter"></ul>
  18. </div>
  19. <script>
  20. function myLoadFilter(data, parent){
  21. var state = $.data(this, 'tree');
  22. function setData(){
  23. var serno = 1;
  24. var todo = [];
  25. for(var i=0; i<data.length; i++){
  26. todo.push(data[i]);
  27. }
  28. while(todo.length){
  29. var node = todo.shift();
  30. if (node.id == undefined){
  31. node.id = '_node_' + (serno++);
  32. }
  33. if (node.children){
  34. node.state = 'closed';
  35. node.children1 = node.children;
  36. node.children = undefined;
  37. todo = todo.concat(node.children1);
  38. }
  39. }
  40. state.tdata = data;
  41. }
  42. function find(id){
  43. var data = state.tdata;
  44. var cc = [data];
  45. while(cc.length){
  46. var c = cc.shift();
  47. for(var i=0; i<c.length; i++){
  48. var node = c[i];
  49. if (node.id == id){
  50. return node;
  51. } else if (node.children1){
  52. cc.push(node.children1);
  53. }
  54. }
  55. }
  56. return null;
  57. }
  58. setData();
  59. var t = $(this);
  60. var opts = t.tree('options');
  61. opts.onBeforeExpand = function(node){
  62. var n = find(node.id);
  63. if (n.children && n.children.length){return}
  64. if (n.children1){
  65. var filter = opts.loadFilter;
  66. opts.loadFilter = function(data){return data;};
  67. t.tree('append',{
  68. parent:node.target,
  69. data:n.children1
  70. });
  71. opts.loadFilter = filter;
  72. n.children = n.children1;
  73. }
  74. };
  75. return data;
  76. }
  77. </script>
  78. </body>
  79. </html>