選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

lutable.js 7.3KB

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