UNPKG

7.61 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.bootstrapTableFixedColumns = 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 zhixin wen <wenzhixin2010@gmail.com>
91 */
92
93 (function ($) {
94 $.extend($.fn.bootstrapTable.defaults, {
95 fixedColumns: false,
96 fixedNumber: 1
97 });
98
99 $.BootstrapTable = function (_$$BootstrapTable) {
100 _inherits(_class, _$$BootstrapTable);
101
102 function _class() {
103 _classCallCheck(this, _class);
104
105 return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
106 }
107
108 _createClass(_class, [{
109 key: 'fitHeader',
110 value: function fitHeader() {
111 var _get2;
112
113 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
114 args[_key] = arguments[_key];
115 }
116
117 (_get2 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'fitHeader', this)).call.apply(_get2, [this].concat(args));
118
119 if (!this.options.fixedColumns) {
120 return;
121 }
122
123 if (this.$el.is(':hidden')) {
124 return;
125 }
126
127 this.$container.find('.fixed-table-header-columns').remove();
128 this.$fixedHeader = $('<div class="fixed-table-header-columns"></div>');
129 this.$fixedHeader.append(this.$tableHeader.find('>table').clone(true));
130 this.$tableHeader.after(this.$fixedHeader);
131
132 var width = this.getFixedColumnsWidth();
133
134 this.$fixedHeader.css({
135 top: 0,
136 width: width,
137 height: this.$tableHeader.outerHeight(true)
138 });
139
140 this.initFixedColumnsBody();
141
142 this.$fixedBody.css({
143 top: this.$tableHeader.outerHeight(true),
144 width: width,
145 height: this.$tableBody.outerHeight(true) - 1
146 });
147
148 this.initFixedColumnsEvents();
149 }
150 }, {
151 key: 'initBody',
152 value: function initBody() {
153 var _get3;
154
155 for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
156 args[_key2] = arguments[_key2];
157 }
158
159 (_get3 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initBody', this)).call.apply(_get3, [this].concat(args));
160
161 if (!this.options.fixedColumns) {
162 return;
163 }
164
165 if (this.options.showHeader && this.options.height) {
166 return;
167 }
168
169 this.initFixedColumnsBody();
170
171 this.$fixedBody.css({
172 top: 0,
173 width: this.getFixedColumnsWidth(),
174 height: this.$tableHeader.outerHeight(true) + this.$tableBody.outerHeight(true)
175 });
176
177 this.initFixedColumnsEvents();
178 }
179 }, {
180 key: 'initFixedColumnsBody',
181 value: function initFixedColumnsBody() {
182 this.$container.find('.fixed-table-body-columns').remove();
183 this.$fixedBody = $('<div class="fixed-table-body-columns"></div>');
184 this.$fixedBody.append(this.$tableBody.find('>table').clone(true));
185 this.$tableBody.after(this.$fixedBody);
186 }
187 }, {
188 key: 'getFixedColumnsWidth',
189 value: function getFixedColumnsWidth() {
190 var visibleFields = this.getVisibleFields();
191 var width = 0;
192
193 for (var i = 0; i < this.options.fixedNumber; i++) {
194 width += this.$header.find('th[data-field="' + visibleFields[i] + '"]').outerWidth(true);
195 }
196
197 return width + 1;
198 }
199 }, {
200 key: 'initFixedColumnsEvents',
201 value: function initFixedColumnsEvents() {
202 var _this2 = this;
203
204 // events
205 this.$tableBody.off('scroll.fixed-columns').on('scroll.fixed-columns', function (e) {
206 _this2.$fixedBody.find('table').css('top', -$(e.currentTarget).scrollTop());
207 });
208
209 this.$body.find('> tr[data-index]').off('hover').hover(function (e) {
210 var index = $(e.currentTarget).data('index');
211 _this2.$fixedBody.find('tr[data-index="' + index + '"]').css('background-color', $(e.currentTarget).css('background-color'));
212 }, function (e) {
213 var index = $(e.currentTarget).data('index');
214 var $tr = _this2.$fixedBody.find('tr[data-index="' + index + '"]');
215 $tr.attr('style', $tr.attr('style').replace(/background-color:.*;/, ''));
216 });
217
218 this.$fixedBody.find('tr[data-index]').off('hover').hover(function (e) {
219 var index = $(e.currentTarget).data('index');
220 _this2.$body.find('tr[data-index="' + index + '"]').css('background-color', $(e.currentTarget).css('background-color'));
221 }, function (e) {
222 var index = $(e.currentTarget).data('index');
223 var $tr = _this2.$body.find('> tr[data-index="' + index + '"]');
224 $tr.attr('style', $tr.attr('style').replace(/background-color:.*;/, ''));
225 });
226 }
227 }]);
228
229 return _class;
230 }($.BootstrapTable);
231 })(jQuery);
232});
\No newline at end of file