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.

lutable.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Created by robin on 11/3/15.
  3. */
  4. angular.module('luticateUtils')
  5. .directive('luTable', ['dialogs', 'luticateDialogErrorHelper',
  6. function(dialogs, luticateDialogErrorHelper) {
  7. return {
  8. restrict: 'E',
  9. templateUrl: "/luticate/lutable.html",
  10. link: function ($scope, element, attrs) {
  11. $scope.page = 0;
  12. $scope.perPage = 15;
  13. $scope.items = [];
  14. $scope.pages = [];
  15. $scope.selectedItems = [];
  16. var promiseLoadItems = {
  17. id: "promiseLoadItems",
  18. groups: ['itemList']
  19. };
  20. var promiseDelItems = {
  21. id: "promiseDelItems",
  22. loadGroups: ['itemList']
  23. };
  24. $scope.loadPage = function (page) {
  25. $scope.getLoadPagePromise(page, $scope.perPage, promiseLoadItems)
  26. .then(function (items) {
  27. $scope.page = page;
  28. $scope.items = items;
  29. $scope.pages = [];
  30. var start = Math.max(0, $scope.page - 5);
  31. var end = Math.min(start + 10, (items.Count / $scope.perPage) + (items.Count % $scope.perPage == 0 ? -1 : 0));
  32. for (var i = start; i < end; ++i) {
  33. $scope.pages.push(i);
  34. }
  35. }, function (error) {
  36. });
  37. };
  38. $scope.displayItem = function (item) {
  39. var ctrl = $scope.getEditController();
  40. dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', item)
  41. .result.then(function (data) {
  42. $scope.loadPage($scope.page);
  43. });
  44. };
  45. $scope.removeItems = function () {
  46. if ($scope.selectedItems.length == 0) {
  47. $scope.loadPage($scope.page);
  48. return;
  49. }
  50. $scope.getDelPromise($scope.selectedItems[0], promiseDelItems)
  51. .then(function (data) {
  52. $scope.selectedItems.splice(0, 1);
  53. $scope.removeItems();
  54. }, function (error) {
  55. luticateDialogErrorHelper.errorDialog(error)
  56. .result.then(function (data) {
  57. $scope.loadPage($scope.page);
  58. }, function (error) {
  59. });
  60. });
  61. };
  62. $scope.addItem = function () {
  63. var ctrl = $scope.getEditController();
  64. dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', null)
  65. .result.then(function (data) {
  66. $scope.loadPage($scope.page);
  67. });
  68. };
  69. $scope.toggleSelectedItem = function (item) {
  70. var id = $scope.getItemId(item);
  71. var idx = $scope.selectedItems.indexOf(id);
  72. if (idx > -1) {
  73. $scope.selectedItems.splice(idx, 1);
  74. }
  75. else {
  76. $scope.selectedItems.push(id);
  77. }
  78. };
  79. $scope.toggleSelectAll = function () {
  80. if ($scope.selectedItems.length == $scope.items.Data.length) {
  81. $scope.selectedItems = [];
  82. }
  83. else {
  84. $scope.selectedItems = [];
  85. for (var i = 0; i < $scope.items.Data.length; ++i) {
  86. $scope.selectedItems.push($scope.getItemId($scope.items.Data[i]));
  87. }
  88. }
  89. };
  90. $scope.isItemChecked = function(item)
  91. {
  92. return $scope.selectedItems.indexOf($scope.getItemId(item)) > -1;
  93. };
  94. if (!$scope.getItemId) {
  95. $scope.getItemId = function(item)
  96. {
  97. return item.Id;
  98. };
  99. }
  100. $scope.loadPage($scope.page);
  101. }
  102. };
  103. }
  104. ]);
  105. angular.module('luticateUtils').run(['$templateCache', function($templateCache)
  106. {
  107. $templateCache.put('/luticate/lutable.html', '<div lu-busy="itemList">' +
  108. ' <table class="col-sm-12 table table-hover">' +
  109. ' <thead>' +
  110. ' <tr>' +
  111. ' <th>' +
  112. ' <input type="checkbox" ng-click="toggleSelectAll()"' +
  113. ' ng-checked="selectedItems.length == items.Data.length && items.Data.length != 0">' +
  114. ' </th>' +
  115. ' <th class="col-sm-{{ col.width }}" ng-repeat="col in columns">{{ col.name }}</th>' +
  116. '</tr>' +
  117. '</thead>' +
  118. '<tbody>' +
  119. '<tr ng-repeat="item in items.Data" style="cursor: pointer" ng-click="displayItem(item)">' +
  120. ' <td>' +
  121. ' <input name="selectedItems[]" type="checkbox" ng-checked="isItemChecked(item)"' +
  122. ' ng-click="$event.stopPropagation();toggleSelectedItem(item)" >' +
  123. ' </td>' +
  124. ' <td ng-repeat="col in columns">{{ col.getValue(item) }}</td>' +
  125. '</tr>' +
  126. '</tbody>' +
  127. '</table>' +
  128. '<div class="col-sm-12 text-center">' +
  129. ' <a class="{{ p == page ? \'pagination-current\' : \'pagination-not-current\'}}" href="" ng-repeat="p in pages" ng-click="loadPage(p)">{{ p + 1 }}&nbsp;</a>' +
  130. '</div>' +
  131. '<div class="col-sm-12">' +
  132. ' <button class="btn btn-default" type="button" ng-click="removeItems()" ng-disabled="selectedItems.length == 0">' +
  133. ' <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove' +
  134. ' </button>' +
  135. ' <button class="btn btn-default" type="button" ng-click="addItem()">' +
  136. ' <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add' +
  137. ' </button>' +
  138. ' </div>' +
  139. ' </div>');
  140. }]);