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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. if ($scope.tableOptions.page == null) {
  18. $scope.tableOptions.page = 0;
  19. }
  20. if ($scope.tableOptions.perPage == null) {
  21. $scope.tableOptions.perPage = 15;
  22. }
  23. if ($scope.tableOptions.checkedItems == null) {
  24. $scope.tableOptions.checkedItems = [];
  25. }
  26. if ($scope.tableOptions.canCheck == null) {
  27. $scope.tableOptions.canCheck = function()
  28. {
  29. return false;
  30. }
  31. }
  32. if ($scope.tableOptions.onItemClicked == null) {
  33. $scope.itemsCursor = "";
  34. $scope.tableOptions.onItemClicked = function(item, scope)
  35. {
  36. }
  37. }
  38. else {
  39. $scope.itemsCursor = "pointer";
  40. }
  41. if ($scope.tableOptions.onItemChecked == null) {
  42. $scope.tableOptions.onItemChecked = function(item, checked, scope)
  43. {
  44. return true;
  45. }
  46. }
  47. if (!$scope.tableOptions.getItemId) {
  48. $scope.tableOptions.getItemId = function(item)
  49. {
  50. return item.Id;
  51. };
  52. }
  53. $scope.loadPage = function (page) {
  54. var promiseLoadItems = {
  55. id: "promiseLoadItems",
  56. groups: ['itemList']
  57. };
  58. $scope.tableOptions.getLoadPagePromise(page, $scope.tableOptions.perPage, promiseLoadItems)
  59. .then(function (items) {
  60. $scope.tableOptions.page = page;
  61. $scope.items = items;
  62. $scope.pages = [];
  63. var start = Math.max(0, $scope.tableOptions.page - 5);
  64. var end = Math.min(start + 10, (items.Count / $scope.tableOptions.perPage)
  65. + (items.Count % $scope.tableOptions.perPage == 0 ? -1 : 0));
  66. for (var i = start; i < end; ++i) {
  67. $scope.pages.push(i);
  68. }
  69. }, function (error) {
  70. });
  71. };
  72. $scope.toggleCheckedItem = function (item) {
  73. var id = $scope.tableOptions.getItemId(item);
  74. var idx = $scope.tableOptions.checkedItems.indexOf(id);
  75. if (idx > -1) {
  76. $scope.tableOptions.onItemChecked(item, false, $scope);
  77. $scope.tableOptions.checkedItems.splice(idx, 1);
  78. }
  79. else {
  80. $scope.tableOptions.onItemChecked(item, true, $scope);
  81. $scope.tableOptions.checkedItems.push(id);
  82. }
  83. };
  84. $scope.toggleCheckAll = function () {
  85. if ($scope.tableOptions.checkedItems.length == $scope.items.Data.length) {
  86. $scope.tableOptions.checkedItems = [];
  87. }
  88. else {
  89. $scope.tableOptions.checkedItems = [];
  90. for (var i = 0; i < $scope.items.Data.length; ++i) {
  91. $scope.tableOptions.checkedItems.push($scope.tableOptions.getItemId($scope.items.Data[i]));
  92. }
  93. }
  94. };
  95. $scope.isItemChecked = function(item)
  96. {
  97. return $scope.tableOptions.checkedItems.indexOf($scope.tableOptions.getItemId(item)) > -1;
  98. };
  99. $scope.onItemClicked = function(item)
  100. {
  101. $scope.tableOptions.onItemClicked(item, $scope);
  102. };
  103. $scope.loadPage($scope.tableOptions.page);
  104. }
  105. };
  106. }
  107. ]);
  108. angular.module('luticateUtils').run(['$templateCache', function($templateCache)
  109. {
  110. $templateCache.put('/luticate/lubasictable.html', '<div lu-busy="itemList">' +
  111. ' <table class="col-sm-12 table table-hover">' +
  112. ' <thead>' +
  113. ' <tr>' +
  114. ' <th ng-show="tableOptions.canCheck()">' +
  115. ' <input type="checkbox" ng-click="toggleCheckAll()"' +
  116. ' ng-checked="tableOptions.checkedItems.length == items.Data.length && items.Data.length != 0">' +
  117. ' </th>' +
  118. ' <th class="col-sm-{{ col.width }}" ng-repeat="col in tableOptions.columns">{{ col.name }}</th>' +
  119. '</tr>' +
  120. '</thead>' +
  121. '<tbody>' +
  122. '<tr ng-repeat="item in items.Data" ng-style="{\'cursor\': itemsCursor}" ng-click="onItemClicked(item)">' +
  123. ' <td ng-show="tableOptions.canCheck()">' +
  124. ' <input name="tableOptions.checkedItems[]" type="checkbox" ng-checked="isItemChecked(item)"' +
  125. ' ng-click="$event.stopPropagation();toggleCheckedItem(item)" >' +
  126. ' </td>' +
  127. ' <td ng-repeat="col in tableOptions.columns">{{ col.getValue(item) }}</td>' +
  128. '</tr>' +
  129. '</tbody>' +
  130. '</table>' +
  131. '<div class="col-sm-12 text-center">' +
  132. ' <a class="{{ p == tableOptions.page ? \'pagination-current\' : \'pagination-not-current\'}}" href="" ng-repeat="p in pages" ng-click="loadPage(p)">{{ p + 1 }}&nbsp;</a>' +
  133. '</div>' +
  134. ' </div>');
  135. }]);