bootstrap-table-angular.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // JavaScript source code
  2. (function () {
  3. if (typeof angular === 'undefined') {
  4. return;
  5. }
  6. angular.module('bsTable', [])
  7. .constant('uiBsTables', {bsTables: {}})
  8. .directive('bsTableControl', ['uiBsTables', function (uiBsTables) {
  9. var CONTAINER_SELECTOR = '.bootstrap-table';
  10. var SCROLLABLE_SELECTOR = '.fixed-table-body';
  11. var SEARCH_SELECTOR = '.search input';
  12. var bsTables = uiBsTables.bsTables;
  13. function getBsTable (el) {
  14. var result;
  15. $.each(bsTables, function (id, bsTable) {
  16. if (!bsTable.$el.closest(CONTAINER_SELECTOR).has(el).length) return;
  17. result = bsTable;
  18. return true;
  19. });
  20. return result;
  21. }
  22. $(window).resize(function () {
  23. $.each(bsTables, function (id, bsTable) {
  24. bsTable.$el.bootstrapTable('resetView');
  25. });
  26. });
  27. function onScroll () {
  28. var bsTable = this;
  29. var state = bsTable.$s.bsTableControl.state;
  30. bsTable.$s.$applyAsync(function () {
  31. state.scroll = bsTable.$el.bootstrapTable('getScrollPosition');
  32. });
  33. }
  34. $(document)
  35. .on('post-header.bs.table', CONTAINER_SELECTOR+' table', function (evt) { // bootstrap-table calls .off('scroll') in initHeader so reattach here
  36. var bsTable = getBsTable(evt.target);
  37. if (!bsTable) return;
  38. bsTable.$el
  39. .closest(CONTAINER_SELECTOR)
  40. .find(SCROLLABLE_SELECTOR)
  41. .on('scroll', onScroll.bind(bsTable));
  42. })
  43. .on('sort.bs.table', CONTAINER_SELECTOR+' table', function (evt, sortName, sortOrder) {
  44. var bsTable = getBsTable(evt.target);
  45. if (!bsTable) return;
  46. var state = bsTable.$s.bsTableControl.state;
  47. bsTable.$s.$applyAsync(function () {
  48. state.sortName = sortName;
  49. state.sortOrder = sortOrder;
  50. });
  51. })
  52. .on('page-change.bs.table', CONTAINER_SELECTOR+' table', function (evt, pageNumber, pageSize) {
  53. var bsTable = getBsTable(evt.target);
  54. if (!bsTable) return;
  55. var state = bsTable.$s.bsTableControl.state;
  56. bsTable.$s.$applyAsync(function () {
  57. state.pageNumber = pageNumber;
  58. state.pageSize = pageSize;
  59. });
  60. })
  61. .on('search.bs.table', CONTAINER_SELECTOR+' table', function (evt, searchText) {
  62. var bsTable = getBsTable(evt.target);
  63. if (!bsTable) return;
  64. var state = bsTable.$s.bsTableControl.state;
  65. bsTable.$s.$applyAsync(function () {
  66. state.searchText = searchText;
  67. });
  68. })
  69. .on('focus blur', CONTAINER_SELECTOR+' '+SEARCH_SELECTOR, function (evt) {
  70. var bsTable = getBsTable(evt.target);
  71. if (!bsTable) return;
  72. var state = bsTable.$s.bsTableControl.state;
  73. bsTable.$s.$applyAsync(function () {
  74. state.searchHasFocus = $(evt.target).is(':focus');
  75. });
  76. });
  77. return {
  78. restrict: 'EA',
  79. scope: {bsTableControl: '='},
  80. link: function ($s, $el) {
  81. var bsTable = bsTables[$s.$id] = {$s: $s, $el: $el};
  82. $s.instantiated = false;
  83. $s.$watch('bsTableControl.options', function (options) {
  84. if (!options) options = $s.bsTableControl.options = {};
  85. var state = $s.bsTableControl.state || {};
  86. if ($s.instantiated) $el.bootstrapTable('destroy');
  87. $el.bootstrapTable(angular.extend(angular.copy(options), state));
  88. $s.instantiated = true;
  89. // Update the UI for state that isn't settable via options
  90. if ('scroll' in state) $el.bootstrapTable('scrollTo', state.scroll);
  91. if ('searchHasFocus' in state) $el.closest(CONTAINER_SELECTOR).find(SEARCH_SELECTOR).focus(); // $el gets detached so have to recompute whole chain
  92. }, true);
  93. $s.$watch('bsTableControl.state', function (state) {
  94. if (!state) state = $s.bsTableControl.state = {};
  95. $el.trigger('directive-updated.bs.table', [state]);
  96. }, true);
  97. $s.$on('$destroy', function () {
  98. delete bsTables[$s.$id];
  99. });
  100. }
  101. };
  102. }])
  103. })();