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

lubasictable.js 7.3KB

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