UNPKG

6.95 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define([], factory);
4 } else if (typeof exports !== "undefined") {
5 factory();
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory();
11 global.bootstrapTableStickyHeader = mod.exports;
12 }
13})(this, function () {
14 'use strict';
15
16 function _classCallCheck(instance, Constructor) {
17 if (!(instance instanceof Constructor)) {
18 throw new TypeError("Cannot call a class as a function");
19 }
20 }
21
22 var _createClass = function () {
23 function defineProperties(target, props) {
24 for (var i = 0; i < props.length; i++) {
25 var descriptor = props[i];
26 descriptor.enumerable = descriptor.enumerable || false;
27 descriptor.configurable = true;
28 if ("value" in descriptor) descriptor.writable = true;
29 Object.defineProperty(target, descriptor.key, descriptor);
30 }
31 }
32
33 return function (Constructor, protoProps, staticProps) {
34 if (protoProps) defineProperties(Constructor.prototype, protoProps);
35 if (staticProps) defineProperties(Constructor, staticProps);
36 return Constructor;
37 };
38 }();
39
40 function _possibleConstructorReturn(self, call) {
41 if (!self) {
42 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
43 }
44
45 return call && (typeof call === "object" || typeof call === "function") ? call : self;
46 }
47
48 var _get = function get(object, property, receiver) {
49 if (object === null) object = Function.prototype;
50 var desc = Object.getOwnPropertyDescriptor(object, property);
51
52 if (desc === undefined) {
53 var parent = Object.getPrototypeOf(object);
54
55 if (parent === null) {
56 return undefined;
57 } else {
58 return get(parent, property, receiver);
59 }
60 } else if ("value" in desc) {
61 return desc.value;
62 } else {
63 var getter = desc.get;
64
65 if (getter === undefined) {
66 return undefined;
67 }
68
69 return getter.call(receiver);
70 }
71 };
72
73 function _inherits(subClass, superClass) {
74 if (typeof superClass !== "function" && superClass !== null) {
75 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
76 }
77
78 subClass.prototype = Object.create(superClass && superClass.prototype, {
79 constructor: {
80 value: subClass,
81 enumerable: false,
82 writable: true,
83 configurable: true
84 }
85 });
86 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
87 }
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
95 (function ($) {
96 var Utils = $.fn.bootstrapTable.utils;
97
98 $.extend($.fn.bootstrapTable.defaults, {
99 stickyHeader: false,
100 stickyHeaderOffsetY: 0
101 });
102
103 var hiddenClass = Utils.bootstrapVersion === 4 ? 'd-none' : 'hidden';
104
105 $.BootstrapTable = function (_$$BootstrapTable) {
106 _inherits(_class, _$$BootstrapTable);
107
108 function _class() {
109 _classCallCheck(this, _class);
110
111 return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
112 }
113
114 _createClass(_class, [{
115 key: 'initHeader',
116 value: function initHeader() {
117 var _get2,
118 _this2 = this;
119
120 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
121 args[_key] = arguments[_key];
122 }
123
124 (_get2 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initHeader', this)).call.apply(_get2, [this].concat(args));
125
126 if (!this.options.stickyHeader) {
127 return;
128 }
129
130 this.$el.before('<div class="sticky-header-container"></div>');
131 this.$el.before('<div class="sticky_anchor_begin"></div>');
132 this.$el.after('<div class="sticky_anchor_end"></div>');
133 this.$header.addClass('sticky-header');
134
135 // clone header just once, to be used as sticky header
136 // deep clone header, using source header affects tbody>td width
137 this.$stickyContainer = this.$tableBody.find('.sticky-header-container');
138 this.$stickyBegin = this.$tableBody.find('.sticky_anchor_begin');
139 this.$stickyEnd = this.$tableBody.find('.sticky_anchor_end');
140 this.$stickyHeader = this.$header.clone(true, true);
141
142 // render sticky on window scroll or resize
143 $(window).on('resize.sticky-header-table', function () {
144 return _this2.renderStickyHeader();
145 });
146 $(window).on('scroll.sticky-header-table', function () {
147 return _this2.renderStickyHeader();
148 });
149 this.$tableBody.off('scroll').on('scroll', function () {
150 return _this2.matchPositionX();
151 });
152 }
153 }, {
154 key: 'renderStickyHeader',
155 value: function renderStickyHeader() {
156 var _this3 = this;
157
158 var top = $(window).scrollTop();
159 // top anchor scroll position, minus header height
160 var start = this.$stickyBegin.offset().top - this.options.stickyHeaderOffsetY;
161 // bottom anchor scroll position, minus header height, minus sticky height
162 var end = this.$stickyEnd.offset().top - this.options.stickyHeaderOffsetY - this.$header.height();
163 // show sticky when top anchor touches header, and when bottom anchor not exceeded
164 if (top > start && top <= end) {
165 // ensure clone and source column widths are the same
166 this.$stickyHeader.find('tr:eq(0)').find('th').each(function (index, el) {
167 $(el).css('min-width', _this3.$header.find('tr:eq(0)').find('th').eq(index).css('width'));
168 });
169 // match bootstrap table style
170 this.$stickyContainer.removeClass(hiddenClass).addClass('fix-sticky fixed-table-container');
171 // stick it in position
172 this.$stickyContainer.css('top', this.options.stickyHeaderOffsetY + 'px');
173 // create scrollable container for header
174 this.$stickyTable = $('<table/>');
175 this.$stickyTable.addClass(this.options.classes);
176 // append cloned header to dom
177 this.$stickyContainer.html(this.$stickyTable.append(this.$stickyHeader));
178 // match clone and source header positions when left-right scroll
179 this.matchPositionX();
180 } else {
181 this.$stickyContainer.removeClass('fix-sticky').addClass(hiddenClass);
182 }
183 }
184 }, {
185 key: 'matchPositionX',
186 value: function matchPositionX() {
187 this.$stickyContainer.scrollLeft(this.$tableBody.scrollLeft());
188 }
189 }]);
190
191 return _class;
192 }($.BootstrapTable);
193 })(jQuery);
194});
\No newline at end of file