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.bootstrapTableSelect2Filter = mod.exports;
|
12 | }
|
13 | })(this, function () {
|
14 | 'use strict';
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 | !function ($) {
|
22 | 'use strict';
|
23 |
|
24 | function getCurrentHeader(that) {
|
25 | var header = that.$header;
|
26 | if (that.options.height) {
|
27 | header = that.$tableHeader;
|
28 | }
|
29 |
|
30 | return header;
|
31 | }
|
32 |
|
33 | function initFilterValues(that) {
|
34 | if (!$.isEmptyObject(that.filterColumnsPartial)) {
|
35 | var $header = getCurrentHeader(that);
|
36 |
|
37 | $.each(that.columns, function (idx, column) {
|
38 | var value = that.filterColumnsPartial[column.field];
|
39 |
|
40 | if (column.filter) {
|
41 | if (column.filter.setFilterValue) {
|
42 | var $filter = $header.find('[data-field=' + column.field + '] .filter');
|
43 | column.filter.setFilterValue($filter, column.field, value);
|
44 | } else {
|
45 | var $ele = $header.find('[data-filter-field=' + column.field + ']');
|
46 | switch (column.filter.type) {
|
47 | case 'input':
|
48 | $ele.val(value);
|
49 | case 'select':
|
50 | $ele.val(value).trigger('change');
|
51 | }
|
52 | }
|
53 | }
|
54 | });
|
55 | }
|
56 | }
|
57 |
|
58 | function createFilter(that, header) {
|
59 | var enableFilter = false,
|
60 | isVisible,
|
61 | html,
|
62 | timeoutId = 0;
|
63 |
|
64 | $.each(that.columns, function (i, column) {
|
65 | isVisible = 'hidden';
|
66 | html = null;
|
67 |
|
68 | if (!column.visible) {
|
69 | return;
|
70 | }
|
71 |
|
72 | if (!column.filter) {
|
73 | html = $('<div class="no-filter"></div>');
|
74 | } else {
|
75 | var filterClass = column.filter.class ? ' ' + column.filter.class : '';
|
76 | html = $('<div style="margin: 0px 2px 2px 2px;" class="filter' + filterClass + '">');
|
77 |
|
78 | if (column.searchable) {
|
79 | enableFilter = true;
|
80 | isVisible = 'visible';
|
81 | }
|
82 |
|
83 | if (column.filter.template) {
|
84 | html.append(column.filter.template(that, column, isVisible));
|
85 | } else {
|
86 | var $filter = $(that.options.filterTemplate[column.filter.type.toLowerCase()](that, column, isVisible));
|
87 |
|
88 | switch (column.filter.type) {
|
89 | case 'input':
|
90 | var cpLock = true;
|
91 | $filter.off('compositionstart').on('compositionstart', function (event) {
|
92 | cpLock = false;
|
93 | });
|
94 |
|
95 | $filter.off('compositionend').on('compositionend', function (event) {
|
96 | cpLock = true;
|
97 | var $input = $(this);
|
98 | clearTimeout(timeoutId);
|
99 | timeoutId = setTimeout(function () {
|
100 | that.onColumnSearch(event, column.field, $input.val());
|
101 | }, that.options.searchTimeOut);
|
102 | });
|
103 |
|
104 | $filter.off('keyup').on('keyup', function (event) {
|
105 | if (cpLock) {
|
106 | var $input = $(this);
|
107 | clearTimeout(timeoutId);
|
108 | timeoutId = setTimeout(function () {
|
109 | that.onColumnSearch(event, column.field, $input.val());
|
110 | }, that.options.searchTimeOut);
|
111 | }
|
112 | });
|
113 |
|
114 | $filter.off('mouseup').on('mouseup', function (event) {
|
115 | var $input = $(this),
|
116 | oldValue = $input.val();
|
117 |
|
118 | if (oldValue === "") {
|
119 | return;
|
120 | }
|
121 |
|
122 | setTimeout(function () {
|
123 | var newValue = $input.val();
|
124 |
|
125 | if (newValue === "") {
|
126 | clearTimeout(timeoutId);
|
127 | timeoutId = setTimeout(function () {
|
128 | that.onColumnSearch(event, column.field, newValue);
|
129 | }, that.options.searchTimeOut);
|
130 | }
|
131 | }, 1);
|
132 | });
|
133 | break;
|
134 | case 'select':
|
135 | $filter.on('select2:select', function (event) {
|
136 | that.onColumnSearch(event, column.field, $(this).val());
|
137 | });
|
138 |
|
139 | $filter.on("select2:unselecting", function (event) {
|
140 | var $select2 = $(this);
|
141 | event.preventDefault();
|
142 | $select2.val(null).trigger('change');
|
143 | that.searchText = undefined;
|
144 | that.onColumnSearch(event, column.field, $select2.val());
|
145 | });
|
146 | break;
|
147 | }
|
148 |
|
149 | html.append($filter);
|
150 | }
|
151 | }
|
152 |
|
153 | $.each(header.children().children(), function (i, tr) {
|
154 | tr = $(tr);
|
155 | if (tr.data('field') === column.field) {
|
156 | tr.find('.fht-cell').append(html);
|
157 | return false;
|
158 | }
|
159 | });
|
160 | });
|
161 |
|
162 | if (!enableFilter) {
|
163 | header.find('.filter').hide();
|
164 | }
|
165 | }
|
166 |
|
167 | function initSelect2(that) {
|
168 | var $header = getCurrentHeader(that);
|
169 |
|
170 | $.each(that.columns, function (idx, column) {
|
171 | if (column.filter && column.filter.type === 'select') {
|
172 | var $selectEle = $header.find('select[data-filter-field="' + column.field + '"]');
|
173 |
|
174 | if ($selectEle.length > 0 && !$selectEle.data().select2) {
|
175 | var select2Opts = {
|
176 | placeholder: "",
|
177 | allowClear: true,
|
178 | data: column.filter.data,
|
179 | dropdownParent: that.$el.closest(".bootstrap-table")
|
180 | };
|
181 |
|
182 | $selectEle.select2(select2Opts);
|
183 | }
|
184 | }
|
185 | });
|
186 | }
|
187 |
|
188 | $.extend($.fn.bootstrapTable.defaults, {
|
189 | filter: false,
|
190 | filterValues: {},
|
191 | filterTemplate: {
|
192 | input: function input(instance, column, isVisible) {
|
193 | return '<input type="text" class="form-control" data-filter-field="' + column.field + '" style="width: 100%; visibility:' + isVisible + '">';
|
194 | },
|
195 | select: function select(instance, column, isVisible) {
|
196 | return '<select data-filter-field="' + column.field + '" style="width: 100%; visibility:' + isVisible + '"></select>';
|
197 | }
|
198 | },
|
199 | onColumnSearch: function onColumnSearch(field, text) {
|
200 | return false;
|
201 | }
|
202 | });
|
203 |
|
204 | $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
|
205 | filter: undefined
|
206 | });
|
207 |
|
208 | $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
|
209 | 'column-search.bs.table': 'onColumnSearch'
|
210 | });
|
211 |
|
212 | var BootstrapTable = $.fn.bootstrapTable.Constructor,
|
213 | _init = BootstrapTable.prototype.init,
|
214 | _initHeader = BootstrapTable.prototype.initHeader,
|
215 | _initSearch = BootstrapTable.prototype.initSearch;
|
216 |
|
217 | BootstrapTable.prototype.init = function () {
|
218 |
|
219 | if (this.options.filter) {
|
220 | var that = this;
|
221 |
|
222 | if (that.options.filterTemplate) {
|
223 | that.options.filterTemplate = $.extend({}, $.fn.bootstrapTable.defaults.filterTemplate, that.options.filterTemplate);
|
224 | }
|
225 |
|
226 | if (!$.isEmptyObject(that.options.filterValues)) {
|
227 | that.filterColumnsPartial = that.options.filterValues;
|
228 | that.options.filterValues = {};
|
229 | }
|
230 |
|
231 | this.$el.on('reset-view.bs.table', function () {
|
232 |
|
233 | if (!that.options.height) {
|
234 | return;
|
235 | }
|
236 |
|
237 |
|
238 | if (that.$tableHeader.find('select').length > 0 || that.$tableHeader.find('input').length > 0) {
|
239 | return;
|
240 | }
|
241 |
|
242 | createFilter(that, that.$tableHeader);
|
243 | }).on('post-header.bs.table', function () {
|
244 | var timeoutId = 0;
|
245 |
|
246 | initSelect2(that);
|
247 | clearTimeout(timeoutId);
|
248 | timeoutId = setTimeout(function () {
|
249 | initFilterValues(that);
|
250 | }, that.options.searchTimeOut - 1000);
|
251 | }).on('column-switch.bs.table', function (field, checked) {
|
252 | initFilterValues(that);
|
253 | });
|
254 | }
|
255 |
|
256 | _init.apply(this, Array.prototype.slice.apply(arguments));
|
257 | };
|
258 |
|
259 | BootstrapTable.prototype.initHeader = function () {
|
260 | _initHeader.apply(this, Array.prototype.slice.apply(arguments));
|
261 | if (this.options.filter) {
|
262 | createFilter(this, this.$header);
|
263 | }
|
264 | };
|
265 |
|
266 | BootstrapTable.prototype.initSearch = function () {
|
267 | var that = this,
|
268 | filterValues = that.filterColumnsPartial;
|
269 |
|
270 |
|
271 | if (that.options.sidePagination === 'client') {
|
272 | this.data = $.grep(this.data, function (row, idx) {
|
273 | for (var field in filterValues) {
|
274 | var column = that.columns[that.fieldsColumnsIndex[field]],
|
275 | filterValue = filterValues[field].toLowerCase(),
|
276 | rowValue = row[field];
|
277 |
|
278 | rowValue = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, that.header.formatters[$.inArray(field, that.header.fields)], [rowValue, row, idx], rowValue);
|
279 |
|
280 | if (column.filterStrictSearch) {
|
281 | if (!($.inArray(field, that.header.fields) !== -1 && (typeof rowValue === 'string' || typeof rowValue === 'number') && rowValue.toString().toLowerCase() === filterValue.toString().toLowerCase())) {
|
282 | return false;
|
283 | }
|
284 | } else {
|
285 | if (!($.inArray(field, that.header.fields) !== -1 && (typeof rowValue === 'string' || typeof rowValue === 'number') && (rowValue + '').toLowerCase().indexOf(filterValue) !== -1)) {
|
286 | return false;
|
287 | }
|
288 | }
|
289 | }
|
290 |
|
291 | return true;
|
292 | });
|
293 | }
|
294 |
|
295 | _initSearch.apply(this, Array.prototype.slice.apply(arguments));
|
296 | };
|
297 |
|
298 | BootstrapTable.prototype.onColumnSearch = function (event, field, value) {
|
299 | if ($.isEmptyObject(this.filterColumnsPartial)) {
|
300 | this.filterColumnsPartial = {};
|
301 | }
|
302 |
|
303 | if (value) {
|
304 | this.filterColumnsPartial[field] = value;
|
305 | } else {
|
306 | delete this.filterColumnsPartial[field];
|
307 | }
|
308 |
|
309 | this.options.pageNumber = 1;
|
310 | this.onSearch(event);
|
311 | this.trigger('column-search', field, value);
|
312 | };
|
313 |
|
314 | BootstrapTable.prototype.setSelect2Data = function (field, data) {
|
315 | var that = this,
|
316 | $header = getCurrentHeader(that),
|
317 | $selectEle = $header.find('select[data-filter-field=\"' + field + '\"]');
|
318 | $selectEle.empty();
|
319 | $selectEle.select2({
|
320 | data: data,
|
321 | placeholder: "",
|
322 | allowClear: true,
|
323 | dropdownParent: that.$el.closest(".bootstrap-table")
|
324 | });
|
325 |
|
326 | $.each(this.columns, function (idx, column) {
|
327 | if (column.field === field) {
|
328 | column.filter.data = data;
|
329 | return false;
|
330 | }
|
331 | });
|
332 | };
|
333 |
|
334 | BootstrapTable.prototype.setFilterValues = function (values) {
|
335 | this.filterColumnsPartial = values;
|
336 | };
|
337 |
|
338 | $.fn.bootstrapTable.methods.push('setSelect2Data');
|
339 | $.fn.bootstrapTable.methods.push('setFilterValues');
|
340 | }(jQuery);
|
341 | }); |
\ | No newline at end of file |