bootstrap-table-fixed-columns.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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.array.index-of.js'), require('core-js/modules/es.array.reverse.js'), require('core-js/modules/es.object.assign.js'), require('core-js/modules/es.object.to-string.js'), require('core-js/modules/es.parse-int.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.array.index-of.js', 'core-js/modules/es.array.reverse.js', 'core-js/modules/es.object.assign.js', 'core-js/modules/es.object.to-string.js', 'core-js/modules/es.parse-int.js', 'jquery'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(null, null, null, null, null, null, null, global.jQuery));
  5. })(this, (function (es_array_concat_js, es_array_find_js, es_array_indexOf_js, es_array_reverse_js, es_object_assign_js, es_object_toString_js, es_parseInt_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 zhixin wen <wenzhixin2010@gmail.com>
  91. */
  92. var Utils = $.fn.bootstrapTable.utils;
  93. // Reasonable defaults
  94. var PIXEL_STEP = 10;
  95. var LINE_HEIGHT = 40;
  96. var PAGE_HEIGHT = 800;
  97. function normalizeWheel(event) {
  98. var sX = 0; // spinX
  99. var sY = 0; // spinY
  100. var pX = 0; // pixelX
  101. var pY = 0; // pixelY
  102. // Legacy
  103. if ('detail' in event) {
  104. sY = event.detail;
  105. }
  106. if ('wheelDelta' in event) {
  107. sY = -event.wheelDelta / 120;
  108. }
  109. if ('wheelDeltaY' in event) {
  110. sY = -event.wheelDeltaY / 120;
  111. }
  112. if ('wheelDeltaX' in event) {
  113. sX = -event.wheelDeltaX / 120;
  114. }
  115. // side scrolling on FF with DOMMouseScroll
  116. if ('axis' in event && event.axis === event.HORIZONTAL_AXIS) {
  117. sX = sY;
  118. sY = 0;
  119. }
  120. pX = sX * PIXEL_STEP;
  121. pY = sY * PIXEL_STEP;
  122. if ('deltaY' in event) {
  123. pY = event.deltaY;
  124. }
  125. if ('deltaX' in event) {
  126. pX = event.deltaX;
  127. }
  128. if ((pX || pY) && event.deltaMode) {
  129. if (event.deltaMode === 1) {
  130. // delta in LINE units
  131. pX *= LINE_HEIGHT;
  132. pY *= LINE_HEIGHT;
  133. } else {
  134. // delta in PAGE units
  135. pX *= PAGE_HEIGHT;
  136. pY *= PAGE_HEIGHT;
  137. }
  138. }
  139. // Fall-back if spin cannot be determined
  140. if (pX && !sX) {
  141. sX = pX < 1 ? -1 : 1;
  142. }
  143. if (pY && !sY) {
  144. sY = pY < 1 ? -1 : 1;
  145. }
  146. return {
  147. spinX: sX,
  148. spinY: sY,
  149. pixelX: pX,
  150. pixelY: pY
  151. };
  152. }
  153. Object.assign($.fn.bootstrapTable.defaults, {
  154. fixedColumns: false,
  155. fixedNumber: 0,
  156. fixedRightNumber: 0
  157. });
  158. $.BootstrapTable = /*#__PURE__*/function (_$$BootstrapTable) {
  159. function _class() {
  160. _classCallCheck(this, _class);
  161. return _callSuper(this, _class, arguments);
  162. }
  163. _inherits(_class, _$$BootstrapTable);
  164. return _createClass(_class, [{
  165. key: "fixedColumnsSupported",
  166. value: function fixedColumnsSupported() {
  167. return this.options.fixedColumns && !this.options.detailView && !this.options.cardView;
  168. }
  169. }, {
  170. key: "initContainer",
  171. value: function initContainer() {
  172. _get(_getPrototypeOf(_class.prototype), "initContainer", this).call(this);
  173. if (!this.fixedColumnsSupported()) {
  174. return;
  175. }
  176. if (this.options.fixedNumber) {
  177. this.$tableContainer.append('<div class="fixed-columns"></div>');
  178. this.$fixedColumns = this.$tableContainer.find('.fixed-columns');
  179. }
  180. if (this.options.fixedRightNumber) {
  181. this.$tableContainer.append('<div class="fixed-columns-right"></div>');
  182. this.$fixedColumnsRight = this.$tableContainer.find('.fixed-columns-right');
  183. }
  184. }
  185. }, {
  186. key: "initBody",
  187. value: function initBody() {
  188. var _get2;
  189. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  190. args[_key] = arguments[_key];
  191. }
  192. (_get2 = _get(_getPrototypeOf(_class.prototype), "initBody", this)).call.apply(_get2, [this].concat(args));
  193. if (this.$fixedColumns && this.$fixedColumns.length) {
  194. this.$fixedColumns.toggle(this.fixedColumnsSupported());
  195. }
  196. if (this.$fixedColumnsRight && this.$fixedColumnsRight.length) {
  197. this.$fixedColumnsRight.toggle(this.fixedColumnsSupported());
  198. }
  199. if (!this.fixedColumnsSupported()) {
  200. return;
  201. }
  202. if (this.options.showHeader && this.options.height) {
  203. return;
  204. }
  205. this.initFixedColumnsBody();
  206. this.initFixedColumnsEvents();
  207. }
  208. }, {
  209. key: "trigger",
  210. value: function trigger() {
  211. var _get3;
  212. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  213. args[_key2] = arguments[_key2];
  214. }
  215. (_get3 = _get(_getPrototypeOf(_class.prototype), "trigger", this)).call.apply(_get3, [this].concat(args));
  216. if (!this.fixedColumnsSupported()) {
  217. return;
  218. }
  219. if (args[0] === 'post-header') {
  220. this.initFixedColumnsHeader();
  221. } else if (args[0] === 'scroll-body') {
  222. if (this.needFixedColumns && this.options.fixedNumber) {
  223. this.$fixedBody.scrollTop(this.$tableBody.scrollTop());
  224. }
  225. if (this.needFixedColumns && this.options.fixedRightNumber) {
  226. this.$fixedBodyRight.scrollTop(this.$tableBody.scrollTop());
  227. }
  228. }
  229. }
  230. }, {
  231. key: "updateSelected",
  232. value: function updateSelected() {
  233. var _this = this;
  234. _get(_getPrototypeOf(_class.prototype), "updateSelected", this).call(this);
  235. if (!this.fixedColumnsSupported()) {
  236. return;
  237. }
  238. this.$tableBody.find('tr').each(function (i, el) {
  239. var $el = $(el);
  240. var index = $el.data('index');
  241. var classes = $el.attr('class');
  242. var inputSelector = "[name=\"".concat(_this.options.selectItemName, "\"]");
  243. var $input = $el.find(inputSelector);
  244. if (typeof index === 'undefined') {
  245. return;
  246. }
  247. var updateFixedBody = function updateFixedBody($fixedHeader, $fixedBody) {
  248. var $tr = $fixedBody.find("tr[data-index=\"".concat(index, "\"]"));
  249. $tr.attr('class', classes);
  250. if ($input.length) {
  251. $tr.find(inputSelector).prop('checked', $input.prop('checked'));
  252. }
  253. if (_this.$selectAll.length) {
  254. $fixedHeader.add($fixedBody).find('[name="btSelectAll"]').prop('checked', _this.$selectAll.prop('checked'));
  255. }
  256. };
  257. if (_this.$fixedBody && _this.options.fixedNumber) {
  258. updateFixedBody(_this.$fixedHeader, _this.$fixedBody);
  259. }
  260. if (_this.$fixedBodyRight && _this.options.fixedRightNumber) {
  261. updateFixedBody(_this.$fixedHeaderRight, _this.$fixedBodyRight);
  262. }
  263. });
  264. }
  265. }, {
  266. key: "hideLoading",
  267. value: function hideLoading() {
  268. _get(_getPrototypeOf(_class.prototype), "hideLoading", this).call(this);
  269. if (this.needFixedColumns && this.options.fixedNumber) {
  270. this.$fixedColumns.find('.fixed-table-loading').hide();
  271. }
  272. if (this.needFixedColumns && this.options.fixedRightNumber) {
  273. this.$fixedColumnsRight.find('.fixed-table-loading').hide();
  274. }
  275. }
  276. }, {
  277. key: "initFixedColumnsHeader",
  278. value: function initFixedColumnsHeader() {
  279. var _this2 = this;
  280. if (this.options.height) {
  281. this.needFixedColumns = this.$tableHeader.outerWidth(true) < this.$tableHeader.find('table').outerWidth(true);
  282. } else {
  283. this.needFixedColumns = this.$tableBody.outerWidth(true) < this.$tableBody.find('table').outerWidth(true);
  284. }
  285. var initFixedHeader = function initFixedHeader($fixedColumns, isRight) {
  286. $fixedColumns.find('.fixed-table-header').remove();
  287. $fixedColumns.append(_this2.$tableHeader.clone(true));
  288. $fixedColumns.css({
  289. width: _this2.getFixedColumnsWidth(isRight)
  290. });
  291. return $fixedColumns.find('.fixed-table-header');
  292. };
  293. if (this.needFixedColumns && this.options.fixedNumber) {
  294. this.$fixedHeader = initFixedHeader(this.$fixedColumns);
  295. this.$fixedHeader.css('margin-right', '');
  296. } else if (this.$fixedColumns) {
  297. this.$fixedColumns.html('').css('width', '');
  298. }
  299. if (this.needFixedColumns && this.options.fixedRightNumber) {
  300. this.$fixedHeaderRight = initFixedHeader(this.$fixedColumnsRight, true);
  301. this.$fixedHeaderRight.scrollLeft(this.$fixedHeaderRight.find('table').width());
  302. } else if (this.$fixedColumnsRight) {
  303. this.$fixedColumnsRight.html('').css('width', '');
  304. }
  305. this.initFixedColumnsBody();
  306. this.initFixedColumnsEvents();
  307. }
  308. }, {
  309. key: "initFixedColumnsBody",
  310. value: function initFixedColumnsBody() {
  311. var _this3 = this;
  312. var initFixedBody = function initFixedBody($fixedColumns, $fixedHeader) {
  313. $fixedColumns.find('.fixed-table-body').remove();
  314. $fixedColumns.append(_this3.$tableBody.clone(true));
  315. $fixedColumns.find('.fixed-table-body table').removeAttr('id');
  316. var $fixedBody = $fixedColumns.find('.fixed-table-body');
  317. var tableBody = _this3.$tableBody.get(0);
  318. var scrollHeight = tableBody.scrollWidth > tableBody.clientWidth ? Utils.getScrollBarWidth() : 0;
  319. var height = _this3.$tableContainer.outerHeight(true) - scrollHeight - 1;
  320. $fixedColumns.css({
  321. height: height
  322. });
  323. $fixedBody.css({
  324. height: height - $fixedHeader.height()
  325. });
  326. return $fixedBody;
  327. };
  328. if (this.needFixedColumns && this.options.fixedNumber) {
  329. this.$fixedBody = initFixedBody(this.$fixedColumns, this.$fixedHeader);
  330. }
  331. if (this.needFixedColumns && this.options.fixedRightNumber) {
  332. this.$fixedBodyRight = initFixedBody(this.$fixedColumnsRight, this.$fixedHeaderRight);
  333. this.$fixedBodyRight.scrollLeft(this.$fixedBodyRight.find('table').width());
  334. this.$fixedBodyRight.css('overflow-y', this.options.height ? 'auto' : 'hidden');
  335. }
  336. }
  337. }, {
  338. key: "getFixedColumnsWidth",
  339. value: function getFixedColumnsWidth(isRight) {
  340. var visibleFields = this.getVisibleFields();
  341. var width = 0;
  342. var fixedNumber = this.options.fixedNumber;
  343. var marginRight = 0;
  344. if (isRight) {
  345. visibleFields = visibleFields.reverse();
  346. fixedNumber = this.options.fixedRightNumber;
  347. marginRight = parseInt(this.$tableHeader.css('margin-right'), 10);
  348. }
  349. for (var i = 0; i < fixedNumber; i++) {
  350. width += this.$header.find("th[data-field=\"".concat(visibleFields[i], "\"]")).outerWidth(true);
  351. }
  352. return width + marginRight + 1;
  353. }
  354. }, {
  355. key: "initFixedColumnsEvents",
  356. value: function initFixedColumnsEvents() {
  357. var _this4 = this;
  358. var toggleHover = function toggleHover(e, toggle) {
  359. var tr = "tr[data-index=\"".concat($(e.currentTarget).data('index'), "\"]");
  360. var $trs = _this4.$tableBody.find(tr);
  361. if (_this4.$fixedBody) {
  362. $trs = $trs.add(_this4.$fixedBody.find(tr));
  363. }
  364. if (_this4.$fixedBodyRight) {
  365. $trs = $trs.add(_this4.$fixedBodyRight.find(tr));
  366. }
  367. $trs.css('background-color', toggle ? $(e.currentTarget).css('background-color') : '');
  368. };
  369. this.$tableBody.find('tr').hover(function (e) {
  370. toggleHover(e, true);
  371. }, function (e) {
  372. toggleHover(e, false);
  373. });
  374. var isFirefox = typeof navigator !== 'undefined' && navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  375. var mousewheel = isFirefox ? 'DOMMouseScroll' : 'mousewheel';
  376. var updateScroll = function updateScroll(e, fixedBody) {
  377. var normalized = normalizeWheel(e);
  378. var deltaY = Math.ceil(normalized.pixelY);
  379. var top = _this4.$tableBody.scrollTop() + deltaY;
  380. if (deltaY < 0 && top > 0 || deltaY > 0 && top < fixedBody.scrollHeight - fixedBody.clientHeight) {
  381. e.preventDefault();
  382. }
  383. _this4.$tableBody.scrollTop(top);
  384. if (_this4.$fixedBody) {
  385. _this4.$fixedBody.scrollTop(top);
  386. }
  387. if (_this4.$fixedBodyRight) {
  388. _this4.$fixedBodyRight.scrollTop(top);
  389. }
  390. };
  391. if (this.needFixedColumns && this.options.fixedNumber) {
  392. this.$fixedBody.find('tr').hover(function (e) {
  393. toggleHover(e, true);
  394. }, function (e) {
  395. toggleHover(e, false);
  396. });
  397. this.$fixedBody[0].addEventListener(mousewheel, function (e) {
  398. updateScroll(e, _this4.$fixedBody[0]);
  399. });
  400. }
  401. if (this.needFixedColumns && this.options.fixedRightNumber) {
  402. this.$fixedBodyRight.find('tr').hover(function (e) {
  403. toggleHover(e, true);
  404. }, function (e) {
  405. toggleHover(e, false);
  406. });
  407. this.$fixedBodyRight.off('scroll').on('scroll', function () {
  408. var top = _this4.$fixedBodyRight.scrollTop();
  409. _this4.$tableBody.scrollTop(top);
  410. if (_this4.$fixedBody) {
  411. _this4.$fixedBody.scrollTop(top);
  412. }
  413. });
  414. }
  415. if (this.options.filterControl) {
  416. $(this.$fixedColumns).off('keyup change').on('keyup change', function (e) {
  417. var $target = $(e.target);
  418. var value = $target.val();
  419. var field = $target.parents('th').data('field');
  420. var $coreTh = _this4.$header.find("th[data-field=\"".concat(field, "\"]"));
  421. if ($target.is('input')) {
  422. $coreTh.find('input').val(value);
  423. } else if ($target.is('select')) {
  424. var $select = $coreTh.find('select');
  425. $select.find('option[selected]').removeAttr('selected');
  426. $select.find("option[value=\"".concat(value, "\"]")).attr('selected', true);
  427. }
  428. _this4.triggerSearch();
  429. });
  430. }
  431. }
  432. }, {
  433. key: "renderStickyHeader",
  434. value: function renderStickyHeader() {
  435. if (!this.options.stickyHeader) {
  436. return;
  437. }
  438. this.$stickyContainer = this.$container.find('.sticky-header-container');
  439. _get(_getPrototypeOf(_class.prototype), "renderStickyHeader", this).call(this);
  440. if (this.needFixedColumns && this.options.fixedNumber) {
  441. this.$fixedColumns.css('z-index', 101).find('.sticky-header-container').css('right', '').width(this.$fixedColumns.outerWidth());
  442. }
  443. if (this.needFixedColumns && this.options.fixedRightNumber) {
  444. var $stickyHeaderContainerRight = this.$fixedColumnsRight.find('.sticky-header-container');
  445. this.$fixedColumnsRight.css('z-index', 101);
  446. $stickyHeaderContainerRight.css('left', '').scrollLeft($stickyHeaderContainerRight.find('.table').outerWidth()).width(this.$fixedColumnsRight.outerWidth());
  447. }
  448. }
  449. }, {
  450. key: "matchPositionX",
  451. value: function matchPositionX() {
  452. if (!this.options.stickyHeader) {
  453. return;
  454. }
  455. this.$stickyContainer.eq(0).scrollLeft(this.$tableBody.scrollLeft());
  456. }
  457. }]);
  458. }($.BootstrapTable);
  459. }));