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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 true;
  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.getItemId) {
  69. $scope.tableOptions.getItemId = function(item)
  70. {
  71. return item.Id;
  72. };
  73. }
  74. $scope.loadPage = function (page) {
  75. $scope.tableOptions.checkedItems = [];
  76. var promiseLoadItems = {
  77. id: "promiseLoadItems",
  78. groups: [$scope.tableOptions.luBusy.group]
  79. };
  80. $scope.tableOptions.getLoadPagePromise(page, $scope.tableOptions.perPage, promiseLoadItems)
  81. .then(function (items) {
  82. $scope.tableOptions.page = page;
  83. $scope.items = items;
  84. $scope.pages = [];
  85. var start = Math.max(0, $scope.tableOptions.page - 5);
  86. var end = Math.min(start + 10, (items.Count / $scope.tableOptions.perPage)
  87. + (items.Count % $scope.tableOptions.perPage == 0 ? -1 : 0));
  88. for (var i = start; i < end; ++i) {
  89. $scope.pages.push(i);
  90. }
  91. }, function (error) {
  92. });
  93. };
  94. $scope.toggleCheckedItem = function (item) {
  95. var id = $scope.tableOptions.getItemId(item);
  96. var idx = $scope.tableOptions.checkedItems.indexOf(id);
  97. if (idx > -1) {
  98. $scope.tableOptions.onItemChecked(item, false);
  99. $scope.tableOptions.checkedItems.splice(idx, 1);
  100. }
  101. else {
  102. $scope.tableOptions.onItemChecked(item, true);
  103. $scope.tableOptions.checkedItems.push(id);
  104. }
  105. };
  106. $scope.toggleCheckAll = function () {
  107. if ($scope.tableOptions.checkedItems.length == $scope.items.Data.length) {
  108. $scope.tableOptions.checkedItems = [];
  109. }
  110. else {
  111. $scope.tableOptions.checkedItems = [];
  112. for (var i = 0; i < $scope.items.Data.length; ++i) {
  113. $scope.tableOptions.checkedItems.push($scope.tableOptions.getItemId($scope.items.Data[i]));
  114. }
  115. }
  116. };
  117. $scope.isItemChecked = function(item)
  118. {
  119. return $scope.tableOptions.checkedItems.indexOf($scope.tableOptions.getItemId(item)) > -1;
  120. };
  121. $scope.onItemClicked = function(item)
  122. {
  123. $scope.tableOptions.onItemClicked(item);
  124. };
  125. $scope.loadPage($scope.tableOptions.page);
  126. }
  127. };
  128. }
  129. ]);
  130. angular.module('luticateUtils').run(['$templateCache', function($templateCache)
  131. {
  132. $templateCache.put('/luticate/lubasictable.html', '<div lu-busy="tableOptions.luBusy">' +
  133. ' <table class="col-sm-12 table table-hover">' +
  134. ' <thead>' +
  135. ' <tr>' +
  136. ' <th ng-show="tableOptions.canCheck()">' +
  137. ' <input type="checkbox" ng-click="toggleCheckAll()"' +
  138. ' ng-checked="tableOptions.checkedItems.length == items.Data.length && items.Data.length != 0">' +
  139. ' </th>' +
  140. ' <th class="col-sm-{{ col.width }}" ng-repeat="col in tableOptions.columns">{{ col.name }}</th>' +
  141. '</tr>' +
  142. '</thead>' +
  143. '<tbody>' +
  144. '<tr ng-repeat="item in items.Data" ng-style="{\'cursor\': tableOptions.canClick() ? \'pointer\' : \'\'}" ng-click="onItemClicked(item)">' +
  145. ' <td ng-show="tableOptions.canCheck()">' +
  146. ' <input name="tableOptions.checkedItems[]" type="checkbox" ng-checked="isItemChecked(item)"' +
  147. ' ng-click="$event.stopPropagation();toggleCheckedItem(item)" >' +
  148. ' </td>' +
  149. ' <td ng-repeat="col in tableOptions.columns">{{ col.getValue(item) }}</td>' +
  150. '</tr>' +
  151. '</tbody>' +
  152. '</table>' +
  153. '<div class="col-sm-12 text-center">' +
  154. ' <a class="{{ p == tableOptions.page ? \'pagination-current\' : \'pagination-not-current\'}}" href="" ng-repeat="p in pages" ng-click="loadPage(p)">{{ p + 1 }}&nbsp;</a>' +
  155. '</div>' +
  156. ' </div>');
  157. }]);