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.4KB

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