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

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