Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

lubasictable.js 8.2KB

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