bootstrap-table-angular.js 3.9 KB

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