You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lubasictable.js 9.7KB

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