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 8.9KB

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