bootstrap-table-sticky-header.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('core-js/modules/es.array.concat.js'), require('core-js/modules/es.array.find.js'), require('core-js/modules/es.object.assign.js'), require('core-js/modules/es.object.to-string.js'), require('jquery')) :
  3. typeof define === 'function' && define.amd ? define(['core-js/modules/es.array.concat.js', 'core-js/modules/es.array.find.js', 'core-js/modules/es.object.assign.js', 'core-js/modules/es.object.to-string.js', 'jquery'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(null, null, null, null, global.jQuery));
  5. })(this, (function (es_array_concat_js, es_array_find_js, es_object_assign_js, es_object_toString_js, $) { 'use strict';
  6. function _assertThisInitialized(e) {
  7. if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  8. return e;
  9. }
  10. function _callSuper(t, o, e) {
  11. return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e));
  12. }
  13. function _classCallCheck(a, n) {
  14. if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
  15. }
  16. function _defineProperties(e, r) {
  17. for (var t = 0; t < r.length; t++) {
  18. var o = r[t];
  19. o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
  20. }
  21. }
  22. function _createClass(e, r, t) {
  23. return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
  24. writable: !1
  25. }), e;
  26. }
  27. function _get() {
  28. return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
  29. var p = _superPropBase(e, t);
  30. if (p) {
  31. var n = Object.getOwnPropertyDescriptor(p, t);
  32. return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
  33. }
  34. }, _get.apply(null, arguments);
  35. }
  36. function _getPrototypeOf(t) {
  37. return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
  38. return t.__proto__ || Object.getPrototypeOf(t);
  39. }, _getPrototypeOf(t);
  40. }
  41. function _inherits(t, e) {
  42. if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
  43. t.prototype = Object.create(e && e.prototype, {
  44. constructor: {
  45. value: t,
  46. writable: !0,
  47. configurable: !0
  48. }
  49. }), Object.defineProperty(t, "prototype", {
  50. writable: !1
  51. }), e && _setPrototypeOf(t, e);
  52. }
  53. function _isNativeReflectConstruct() {
  54. try {
  55. var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  56. } catch (t) {}
  57. return (_isNativeReflectConstruct = function () {
  58. return !!t;
  59. })();
  60. }
  61. function _possibleConstructorReturn(t, e) {
  62. if (e && ("object" == typeof e || "function" == typeof e)) return e;
  63. if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
  64. return _assertThisInitialized(t);
  65. }
  66. function _setPrototypeOf(t, e) {
  67. return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
  68. return t.__proto__ = e, t;
  69. }, _setPrototypeOf(t, e);
  70. }
  71. function _superPropBase(t, o) {
  72. for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t)););
  73. return t;
  74. }
  75. function _toPrimitive(t, r) {
  76. if ("object" != typeof t || !t) return t;
  77. var e = t[Symbol.toPrimitive];
  78. if (void 0 !== e) {
  79. var i = e.call(t, r );
  80. if ("object" != typeof i) return i;
  81. throw new TypeError("@@toPrimitive must return a primitive value.");
  82. }
  83. return (String )(t);
  84. }
  85. function _toPropertyKey(t) {
  86. var i = _toPrimitive(t, "string");
  87. return "symbol" == typeof i ? i : i + "";
  88. }
  89. /**
  90. * @author vincent loh <vincent.ml@gmail.com>
  91. * @update J Manuel Corona <jmcg92@gmail.com>
  92. * @update zhixin wen <wenzhixin2010@gmail.com>
  93. */
  94. var Utils = $.fn.bootstrapTable.utils;
  95. Object.assign($.fn.bootstrapTable.defaults, {
  96. stickyHeader: false,
  97. stickyHeaderOffsetY: 0,
  98. stickyHeaderOffsetLeft: 0,
  99. stickyHeaderOffsetRight: 0
  100. });
  101. $.BootstrapTable = /*#__PURE__*/function (_$$BootstrapTable) {
  102. function _class() {
  103. _classCallCheck(this, _class);
  104. return _callSuper(this, _class, arguments);
  105. }
  106. _inherits(_class, _$$BootstrapTable);
  107. return _createClass(_class, [{
  108. key: "initHeader",
  109. value: function initHeader() {
  110. var _get2,
  111. _this = this;
  112. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  113. args[_key] = arguments[_key];
  114. }
  115. (_get2 = _get(_getPrototypeOf(_class.prototype), "initHeader", this)).call.apply(_get2, [this].concat(args));
  116. if (!this.options.stickyHeader) {
  117. return;
  118. }
  119. this.$tableBody.find('.sticky-header-container,.sticky_anchor_begin,.sticky_anchor_end').remove();
  120. this.$el.before('<div class="sticky-header-container"></div>');
  121. this.$el.before('<div class="sticky_anchor_begin"></div>');
  122. this.$el.after('<div class="sticky_anchor_end"></div>');
  123. this.$header.addClass('sticky-header');
  124. // clone header just once, to be used as sticky header
  125. // deep clone header, using source header affects tbody>td width
  126. this.$stickyContainer = this.$tableBody.find('.sticky-header-container');
  127. this.$stickyBegin = this.$tableBody.find('.sticky_anchor_begin');
  128. this.$stickyEnd = this.$tableBody.find('.sticky_anchor_end');
  129. this.$stickyHeader = this.$header.clone(true, true);
  130. // render sticky on window scroll or resize
  131. var resizeEvent = Utils.getEventName('resize.sticky-header-table', this.$el.attr('id'));
  132. var scrollEvent = Utils.getEventName('scroll.sticky-header-table', this.$el.attr('id'));
  133. $(window).off(resizeEvent).on(resizeEvent, function () {
  134. return _this.renderStickyHeader();
  135. });
  136. $(window).off(scrollEvent).on(scrollEvent, function () {
  137. return _this.renderStickyHeader();
  138. });
  139. this.$tableBody.off('scroll').on('scroll', function () {
  140. return _this.matchPositionX();
  141. });
  142. }
  143. }, {
  144. key: "onColumnSearch",
  145. value: function onColumnSearch(_ref) {
  146. var currentTarget = _ref.currentTarget,
  147. keyCode = _ref.keyCode;
  148. _get(_getPrototypeOf(_class.prototype), "onColumnSearch", this).call(this, {
  149. currentTarget: currentTarget,
  150. keyCode: keyCode
  151. });
  152. this.renderStickyHeader();
  153. }
  154. }, {
  155. key: "resetView",
  156. value: function resetView() {
  157. var _get3,
  158. _this2 = this;
  159. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  160. args[_key2] = arguments[_key2];
  161. }
  162. (_get3 = _get(_getPrototypeOf(_class.prototype), "resetView", this)).call.apply(_get3, [this].concat(args));
  163. $('.bootstrap-table.fullscreen').off('scroll').on('scroll', function () {
  164. return _this2.renderStickyHeader();
  165. });
  166. }
  167. }, {
  168. key: "getCaret",
  169. value: function getCaret() {
  170. var _get4;
  171. for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  172. args[_key3] = arguments[_key3];
  173. }
  174. (_get4 = _get(_getPrototypeOf(_class.prototype), "getCaret", this)).call.apply(_get4, [this].concat(args));
  175. if (this.$stickyHeader) {
  176. var $ths = this.$stickyHeader.find('th');
  177. this.$header.find('th').each(function (i, th) {
  178. $ths.eq(i).find('.sortable').attr('class', $(th).find('.sortable').attr('class'));
  179. });
  180. }
  181. }
  182. }, {
  183. key: "horizontalScroll",
  184. value: function horizontalScroll() {
  185. var _this3 = this;
  186. _get(_getPrototypeOf(_class.prototype), "horizontalScroll", this).call(this);
  187. this.$tableBody.on('scroll', function () {
  188. return _this3.matchPositionX();
  189. });
  190. }
  191. }, {
  192. key: "renderStickyHeader",
  193. value: function renderStickyHeader() {
  194. var _this4 = this;
  195. var that = this;
  196. this.$stickyHeader = this.$header.clone(true, true);
  197. if (this.options.filterControl) {
  198. $(this.$stickyHeader).off('keyup change mouseup').on('keyup change mouse', function (e) {
  199. var $target = $(e.target);
  200. var value = $target.val();
  201. var field = $target.parents('th').data('field');
  202. var $coreTh = that.$header.find("th[data-field=\"".concat(field, "\"]"));
  203. if ($target.is('input')) {
  204. $coreTh.find('input').val(value);
  205. } else if ($target.is('select')) {
  206. var $select = $coreTh.find('select');
  207. $select.find('option[selected]').removeAttr('selected');
  208. $select.find("option[value=\"".concat(value, "\"]")).attr('selected', true);
  209. }
  210. that.triggerSearch();
  211. });
  212. }
  213. var top = $(window).scrollTop();
  214. // top anchor scroll position, minus header height
  215. var start = this.$stickyBegin.offset().top - this.options.stickyHeaderOffsetY;
  216. // bottom anchor scroll position, minus header height, minus sticky height
  217. var end = this.$stickyEnd.offset().top - this.options.stickyHeaderOffsetY - this.$header.height();
  218. // show sticky when top anchor touches header, and when bottom anchor not exceeded
  219. if (top > start && top <= end) {
  220. // ensure clone and source column widths are the same
  221. this.$stickyHeader.find('tr').each(function (indexRows, rows) {
  222. $(rows).find('th').each(function (index, el) {
  223. $(el).css('min-width', _this4.$header.find("tr:eq(".concat(indexRows, ")")).find("th:eq(".concat(index, ")")).css('width'));
  224. });
  225. });
  226. // match bootstrap table style
  227. this.$stickyContainer.show().addClass('fix-sticky fixed-table-container');
  228. // stick it in position
  229. var coords = this.$tableBody[0].getBoundingClientRect();
  230. var width = '100%';
  231. var stickyHeaderOffsetLeft = this.options.stickyHeaderOffsetLeft;
  232. var stickyHeaderOffsetRight = this.options.stickyHeaderOffsetRight;
  233. if (!stickyHeaderOffsetLeft) {
  234. stickyHeaderOffsetLeft = coords.left;
  235. }
  236. if (!stickyHeaderOffsetRight) {
  237. width = "".concat(coords.width, "px");
  238. }
  239. if (this.$el.closest('.bootstrap-table').hasClass('fullscreen')) {
  240. stickyHeaderOffsetLeft = 0;
  241. stickyHeaderOffsetRight = 0;
  242. width = '100%';
  243. }
  244. this.$stickyContainer.css('top', "".concat(this.options.stickyHeaderOffsetY, "px"));
  245. this.$stickyContainer.css('left', "".concat(stickyHeaderOffsetLeft, "px"));
  246. this.$stickyContainer.css('right', "".concat(stickyHeaderOffsetRight, "px"));
  247. this.$stickyContainer.css('width', "".concat(width));
  248. // create scrollable container for header
  249. this.$stickyTable = $('<table/>');
  250. this.$stickyTable.addClass(this.options.classes);
  251. // append cloned header to dom
  252. this.$stickyContainer.html(this.$stickyTable.append(this.$stickyHeader));
  253. // match clone and source header positions when left-right scroll
  254. this.matchPositionX();
  255. } else {
  256. this.$stickyContainer.removeClass('fix-sticky').hide();
  257. }
  258. }
  259. }, {
  260. key: "matchPositionX",
  261. value: function matchPositionX() {
  262. this.$stickyContainer.scrollLeft(this.$tableBody.scrollLeft());
  263. }
  264. }]);
  265. }($.BootstrapTable);
  266. }));