Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

lubasictable.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * Created by robin on 11/3/15.
  3. */
  4. angular.module('luticateUtils')
  5. .directive('luBasicTable', ['dialogs', 'luticateDialogErrorHelper',
  6. function(dialogs, luticateDialogErrorHelper) {
  7. return {
  8. restrict: 'E',
  9. scope: {
  10. options: '&'
  11. },
  12. templateUrl: "/luticate/lubasictable.html",
  13. link: function ($scope, element, attrs) {
  14. $scope.items = [];
  15. $scope.pages = [];
  16. $scope.maxPage = 1;
  17. $scope.tableOptions = $scope.options();
  18. $scope.tableOptions.luBasicTableScope = $scope;
  19. if ($scope.tableOptions.tableName == null) {
  20. $scope.tableOptions.tableName = $scope.$id;
  21. }
  22. if (typeof $scope.tableOptions.luBusy == 'string') {
  23. $scope.tableOptions.luBusy = {
  24. group: $scope.tableOptions.luBusy
  25. };
  26. }
  27. if ($scope.tableOptions.luBusy == null) {
  28. $scope.tableOptions.luBusy = {
  29. group: 'luBasicTableItemList_' + $scope.$id
  30. };
  31. }
  32. if ($scope.tableOptions.page == null) {
  33. $scope.tableOptions.page = 0;
  34. }
  35. if ($scope.tableOptions.perPage == null) {
  36. $scope.tableOptions.perPage = 15;
  37. }
  38. if ($scope.tableOptions.query == null) {
  39. $scope.tableOptions.query = "";
  40. }
  41. if ($scope.tableOptions.checkedItems == null) {
  42. $scope.tableOptions.checkedItems = [];
  43. }
  44. if ($scope.tableOptions.canCheck == null) {
  45. $scope.tableOptions.canCheck = function()
  46. {
  47. return false;
  48. }
  49. }
  50. if ($scope.tableOptions.canClick == null) {
  51. $scope.tableOptions.canClick = function(item)
  52. {
  53. return false;
  54. }
  55. }
  56. if ($scope.tableOptions.canFilter == null) {
  57. $scope.tableOptions.canFilter = function()
  58. {
  59. return true;
  60. }
  61. }
  62. var onItemClicked = $scope.tableOptions.onItemClicked;
  63. $scope.tableOptions.onItemClicked = function(item)
  64. {
  65. if ($scope.tableOptions.canClick(item)){
  66. onItemClicked(item);
  67. }
  68. };
  69. if ($scope.tableOptions.onItemChecked == null) {
  70. $scope.tableOptions.onItemChecked = function(item, checked)
  71. {
  72. }
  73. }
  74. if (!$scope.tableOptions.onPageChanged) {
  75. $scope.tableOptions.onPageChanged = function()
  76. {
  77. };
  78. }
  79. if (!$scope.tableOptions.getItemId) {
  80. $scope.tableOptions.getItemId = function(item)
  81. {
  82. return item.Id;
  83. };
  84. }
  85. $scope.loadPage = function (page) {
  86. if (page < 0 || page >= $scope.maxPage) {
  87. return;
  88. }
  89. $scope.tableOptions.checkedItems = [];
  90. var promiseLoadItems = {
  91. id: "promiseLoadItems_" + $scope.$id,
  92. groups: [$scope.tableOptions.luBusy.group]
  93. };
  94. $scope.tableOptions.getLoadPagePromise(page, $scope.tableOptions.perPage,
  95. $scope.tableOptions.query, promiseLoadItems)
  96. .then(function (items) {
  97. $scope.tableOptions.page = page;
  98. $scope.items = items;
  99. $scope.pages = [];
  100. $scope.maxPage = Math.max(items.Count / $scope.tableOptions.perPage, 1);
  101. var start = Math.max(0, $scope.tableOptions.page - 5);
  102. var end = Math.min(start + 10, $scope.maxPage);
  103. for (var i = start; i < end; ++i) {
  104. $scope.pages.push(i);
  105. }
  106. $scope.tableOptions.onPageChanged();
  107. }, luticateDialogErrorHelper.errorDialog);
  108. };
  109. $scope.toggleCheckedItem = function (item) {
  110. var id = $scope.tableOptions.getItemId(item);
  111. $scope.toggleCheckedId(id, item);
  112. };
  113. $scope.toggleCheckedId = function (id, item) {
  114. item = item || $scope.items.Data.find(function(item)
  115. {
  116. return $scope.tableOptions.getItemId(item) == id;
  117. });
  118. var idx = $scope.tableOptions.checkedItems.indexOf(id);
  119. if (idx > -1) {
  120. $scope.tableOptions.onItemChecked(item, false);
  121. $scope.tableOptions.checkedItems.splice(idx, 1);
  122. }
  123. else {
  124. $scope.tableOptions.onItemChecked(item, true);
  125. $scope.tableOptions.checkedItems.push(id);
  126. }
  127. };
  128. $scope.unCheckAll = function()
  129. {
  130. while ($scope.tableOptions.checkedItems.length != 0) {
  131. $scope.toggleCheckedId($scope.tableOptions.checkedItems[0]);
  132. }
  133. };
  134. $scope.checkAll = function()
  135. {
  136. $scope.unCheckAll();
  137. for (var i = 0; i < $scope.items.Data.length; ++i) {
  138. $scope.toggleCheckedItem($scope.items.Data[i]);
  139. }
  140. };
  141. $scope.toggleCheckAll = function () {
  142. if ($scope.tableOptions.checkedItems.length == $scope.items.Data.length) {
  143. $scope.unCheckAll();
  144. }
  145. else {
  146. $scope.checkAll();
  147. }
  148. };
  149. $scope.isItemChecked = function(item)
  150. {
  151. return $scope.tableOptions.checkedItems.indexOf($scope.tableOptions.getItemId(item)) > -1;
  152. };
  153. $scope.onItemClicked = function(item)
  154. {
  155. $scope.tableOptions.onItemClicked(item);
  156. };
  157. $scope.loadPage($scope.tableOptions.page);
  158. }
  159. };
  160. }
  161. ]);
  162. angular.module('luticateUtils').run(['$templateCache', function($templateCache)
  163. {
  164. $templateCache.put('/luticate/lubasictable.html', '<div lu-busy="tableOptions.luBusy">' +
  165. '<div class="col-sm-3 pull-right" ng-show="tableOptions.canFilter()"><input placeholder="Filter" class="form-control" ng-model-options="{debounce: 500}" ' +
  166. 'ng-model="tableOptions.query" ng-change="loadPage(0)"/></div>' +
  167. ' <table class="col-sm-12 table table-hover">' +
  168. ' <thead>' +
  169. ' <tr>' +
  170. ' <th ng-show="tableOptions.canCheck()">' +
  171. ' <input type="checkbox" ng-click="toggleCheckAll()"' +
  172. ' ng-checked="tableOptions.checkedItems.length == items.Data.length && items.Data.length != 0">' +
  173. ' </th>' +
  174. ' <th class="col-sm-{{ col.width }}" ng-repeat="col in tableOptions.columns">{{ col.name }}</th>' +
  175. '</tr>' +
  176. '</thead>' +
  177. '<tbody>' +
  178. '<tr ng-repeat="item in items.Data" ng-style="{\'cursor\': tableOptions.canClick(item) ? \'pointer\' : \'\'}" ng-click="onItemClicked(item)">' +
  179. ' <td ng-show="tableOptions.canCheck()">' +
  180. ' <input name="tableOptions.checkedItems[]" type="checkbox" ng-checked="isItemChecked(item)"' +
  181. ' ng-click="$event.stopPropagation();toggleCheckedItem(item)" >' +
  182. ' </td>' +
  183. ' <td ng-repeat="col in tableOptions.columns">{{ col.getValue(item) }}</td>' +
  184. '</tr>' +
  185. '</tbody>' +
  186. '</table>' +
  187. '<div class="col-sm-12 text-center">' +
  188. '<div class="pagination"><ul>' +
  189. ' <li class="previous">' +
  190. ' <a href="" class="fui-arrow-left" ng-click="loadPage(tableOptions.page - 1)"></a></li>' +
  191. ' <li ng-repeat="p in pages" ng-class="{ \'active\' : p == tableOptions.page}">' +
  192. ' <a href="" ng-click="loadPage(p)">{{ p + 1 }}</a></li>' +
  193. ' <li class="next">' +
  194. ' <a href="" class="fui-arrow-right" ng-click="loadPage(tableOptions.page + 1)"></a></li>' +
  195. '</ul></div></div>' +
  196. ' </div>');
  197. }]);