clientpagination.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Client Side Pagination in TreeGrid - 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>Client Side Pagination in TreeGrid</h2>
  14. <p>This sample shows how to implement client side pagination in TreeGrid.</p>
  15. <div style="margin:20px 0;"></div>
  16. <table id="tg" title="Client Side Pagination" style="width:700px;height:250px"
  17. data-options="
  18. iconCls: 'icon-ok',
  19. rownumbers: true,
  20. animate: true,
  21. collapsible: true,
  22. fitColumns: true,
  23. url: 'treegrid_data2.json',
  24. method: 'get',
  25. idField: 'id',
  26. treeField: 'name',
  27. pagination: true,
  28. pageSize: 2,
  29. pageList: [2,5,10]
  30. ">
  31. <thead>
  32. <tr>
  33. <th data-options="field:'name',width:180">Task Name</th>
  34. <th data-options="field:'persons',width:60,align:'right'">Persons</th>
  35. <th data-options="field:'begin',width:80">Begin Date</th>
  36. <th data-options="field:'end',width:80">End Date</th>
  37. <th data-options="field:'progress',width:120,formatter:formatProgress">Progress</th>
  38. </tr>
  39. </thead>
  40. </table>
  41. <script type="text/javascript">
  42. (function($){
  43. function pagerFilter(data){
  44. if ($.isArray(data)){ // is array
  45. data = {
  46. total: data.length,
  47. rows: data
  48. }
  49. }
  50. var target = this;
  51. var tg = $(target);
  52. var state = tg.data('treegrid');
  53. var opts = tg.treegrid('options');
  54. if (!state.allRows){
  55. state.allRows = data.rows;
  56. }
  57. if (!opts.remoteSort && opts.sortName){
  58. var names = opts.sortName.split(',');
  59. var orders = opts.sortOrder.split(',');
  60. state.allRows.sort(function(r1,r2){
  61. var r = 0;
  62. for(var i=0; i<names.length; i++){
  63. var sn = names[i];
  64. var so = orders[i];
  65. var col = $(target).treegrid('getColumnOption', sn);
  66. var sortFunc = col.sorter || function(a,b){
  67. return a==b ? 0 : (a>b?1:-1);
  68. };
  69. r = sortFunc(r1[sn], r2[sn]) * (so=='asc'?1:-1);
  70. if (r != 0){
  71. return r;
  72. }
  73. }
  74. return r;
  75. });
  76. }
  77. var topRows = [];
  78. var childRows = [];
  79. $.map(state.allRows, function(row){
  80. row._parentId ? childRows.push(row) : topRows.push(row);
  81. row.children = null;
  82. });
  83. data.total = topRows.length;
  84. var pager = tg.treegrid('getPager');
  85. pager.pagination('refresh', {
  86. total: data.total,
  87. pageNumber: opts.pageNumber
  88. });
  89. opts.pageNumber = pager.pagination('options').pageNumber || 1;
  90. var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
  91. var end = start + parseInt(opts.pageSize);
  92. data.rows = topRows.slice(start, end).concat(childRows);
  93. return data;
  94. }
  95. var appendMethod = $.fn.treegrid.methods.append;
  96. var removeMethod = $.fn.treegrid.methods.remove;
  97. var loadDataMethod = $.fn.treegrid.methods.loadData;
  98. $.extend($.fn.treegrid.methods, {
  99. clientPaging: function(jq){
  100. return jq.each(function(){
  101. var tg = $(this);
  102. var state = tg.data('treegrid');
  103. var opts = state.options;
  104. opts.loadFilter = pagerFilter;
  105. var onBeforeLoad = opts.onBeforeLoad;
  106. opts.onBeforeLoad = function(row,param){
  107. state.allRows = null;
  108. return onBeforeLoad.call(this, row, param);
  109. }
  110. var pager = tg.treegrid('getPager');
  111. pager.pagination({
  112. onSelectPage:function(pageNum, pageSize){
  113. opts.pageNumber = pageNum;
  114. opts.pageSize = pageSize;
  115. pager.pagination('refresh',{
  116. pageNumber:pageNum,
  117. pageSize:pageSize
  118. });
  119. tg.treegrid('loadData',state.allRows);
  120. }
  121. });
  122. tg.treegrid('loadData', state.data);
  123. if (opts.url){
  124. tg.treegrid('reload');
  125. }
  126. });
  127. },
  128. loadData: function(jq, data){
  129. jq.each(function(){
  130. $(this).data('treegrid').allRows = null;
  131. });
  132. return loadDataMethod.call($.fn.treegrid.methods, jq, data);
  133. },
  134. append: function(jq, param){
  135. return jq.each(function(){
  136. var state = $(this).data('treegrid');
  137. if (state.options.loadFilter == pagerFilter){
  138. $.map(param.data, function(row){
  139. row._parentId = row._parentId || param.parent;
  140. state.allRows.push(row);
  141. });
  142. $(this).treegrid('loadData', state.allRows);
  143. } else {
  144. appendMethod.call($.fn.treegrid.methods, $(this), param);
  145. }
  146. })
  147. },
  148. remove: function(jq, id){
  149. return jq.each(function(){
  150. if ($(this).treegrid('find', id)){
  151. removeMethod.call($.fn.treegrid.methods, $(this), id);
  152. }
  153. var state = $(this).data('treegrid');
  154. if (state.options.loadFilter == pagerFilter){
  155. for(var i=0; i<state.allRows.length; i++){
  156. if (state.allRows[i][state.options.idField] == id){
  157. state.allRows.splice(i,1);
  158. break;
  159. }
  160. }
  161. $(this).treegrid('loadData', state.allRows);
  162. }
  163. })
  164. },
  165. getAllRows: function(jq){
  166. return jq.data('treegrid').allRows;
  167. }
  168. });
  169. })(jQuery);
  170. function formatProgress(value){
  171. if (value){
  172. var s = '<div style="width:100%;border:1px solid #ccc">' +
  173. '<div style="width:' + value + '%;background:#cc0000;color:#fff">' + value + '%' + '</div>'
  174. '</div>';
  175. return s;
  176. } else {
  177. return '';
  178. }
  179. }
  180. $(function(){
  181. $('#tg').treegrid().treegrid('clientPaging');
  182. })
  183. </script>
  184. </body>
  185. </html>