bootstrap-table-filter-control.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v2.1.2
  5. */
  6. (function ($) {
  7. 'use strict';
  8. var sprintf = $.fn.bootstrapTable.utils.sprintf,
  9. objectKeys = $.fn.bootstrapTable.utils.objectKeys;
  10. var getOptionsFromSelectControl = function (selectControl) {
  11. return selectControl.get(selectControl.length - 1).options;
  12. };
  13. var hideUnusedSelectOptions = function (selectControl, uniqueValues) {
  14. var options = getOptionsFromSelectControl(selectControl);
  15. for (var i = 0; i < options.length; i++) {
  16. if (options[i].value !== "") {
  17. if (!uniqueValues.hasOwnProperty(options[i].value)) {
  18. selectControl.find(sprintf("option[value='%s']", options[i].value)).hide();
  19. } else {
  20. selectControl.find(sprintf("option[value='%s']", options[i].value)).show();
  21. }
  22. }
  23. }
  24. };
  25. var addOptionToSelectControl = function (selectControl, value, text) {
  26. value = $.trim(value);
  27. selectControl = $(selectControl.get(selectControl.length - 1));
  28. if (!existOptionInSelectControl(selectControl, value)) {
  29. selectControl.append($("<option></option>")
  30. .attr("value", value)
  31. .text($('<div />').html(text).text()));
  32. }
  33. };
  34. var sortSelectControl = function (selectControl) {
  35. selectControl = $(selectControl.get(selectControl.length - 1));
  36. var $opts = selectControl.find('option:gt(0)');
  37. $opts.sort(function (a, b) {
  38. a = $(a).text().toLowerCase();
  39. b = $(b).text().toLowerCase();
  40. if ($.isNumeric(a) && $.isNumeric(b)) {
  41. // Convert numerical values from string to float.
  42. a = parseFloat(a);
  43. b = parseFloat(b);
  44. }
  45. return a > b ? 1 : a < b ? -1 : 0;
  46. });
  47. selectControl.find('option:gt(0)').remove();
  48. selectControl.append($opts);
  49. };
  50. var existOptionInSelectControl = function (selectControl, value) {
  51. var options = getOptionsFromSelectControl(selectControl);
  52. for (var i = 0; i < options.length; i++) {
  53. if (options[i].value === value.toString()) {
  54. //The value is not valid to add
  55. return true;
  56. }
  57. }
  58. //If we get here, the value is valid to add
  59. return false;
  60. };
  61. var fixHeaderCSS = function (that) {
  62. that.$tableHeader.css('height', '77px');
  63. };
  64. var getCurrentHeader = function (that) {
  65. var header = that.$header;
  66. if (that.options.height) {
  67. header = that.$tableHeader;
  68. }
  69. return header;
  70. };
  71. var getCurrentSearchControls = function (that) {
  72. var searchControls = 'select, input';
  73. if (that.options.height) {
  74. searchControls = 'table select, table input';
  75. }
  76. return searchControls;
  77. };
  78. var getCursorPosition = function(el) {
  79. if ($.fn.bootstrapTable.utils.isIEBrowser()) {
  80. if ($(el).is('input[type=text]')) {
  81. var pos = 0;
  82. if ('selectionStart' in el) {
  83. pos = el.selectionStart;
  84. } else if ('selection' in document) {
  85. el.focus();
  86. var Sel = document.selection.createRange();
  87. var SelLength = document.selection.createRange().text.length;
  88. Sel.moveStart('character', -el.value.length);
  89. pos = Sel.text.length - SelLength;
  90. }
  91. return pos;
  92. } else {
  93. return -1;
  94. }
  95. } else {
  96. return -1;
  97. }
  98. };
  99. var setCursorPosition = function (el) {
  100. $(el).val(el.value);
  101. };
  102. var copyValues = function (that) {
  103. var header = getCurrentHeader(that),
  104. searchControls = getCurrentSearchControls(that);
  105. that.options.valuesFilterControl = [];
  106. header.find(searchControls).each(function () {
  107. that.options.valuesFilterControl.push(
  108. {
  109. field: $(this).closest('[data-field]').data('field'),
  110. value: $(this).val(),
  111. position: getCursorPosition($(this).get(0))
  112. });
  113. });
  114. };
  115. var setValues = function(that) {
  116. var field = null,
  117. result = [],
  118. header = getCurrentHeader(that),
  119. searchControls = getCurrentSearchControls(that);
  120. if (that.options.valuesFilterControl.length > 0) {
  121. header.find(searchControls).each(function (index, ele) {
  122. field = $(this).closest('[data-field]').data('field');
  123. result = $.grep(that.options.valuesFilterControl, function (valueObj) {
  124. return valueObj.field === field;
  125. });
  126. if (result.length > 0) {
  127. $(this).val(result[0].value);
  128. setCursorPosition($(this).get(0), result[0].position);
  129. }
  130. });
  131. }
  132. };
  133. var collectBootstrapCookies = function cookiesRegex() {
  134. var cookies = [],
  135. foundCookies = document.cookie.match(/(?:bs.table.)(\w*)/g);
  136. if (foundCookies) {
  137. $.each(foundCookies, function (i, cookie) {
  138. if (/./.test(cookie)) {
  139. cookie = cookie.split(".").pop();
  140. }
  141. if ($.inArray(cookie, cookies) === -1) {
  142. cookies.push(cookie);
  143. }
  144. });
  145. return cookies;
  146. }
  147. };
  148. var initFilterSelectControls = function (that) {
  149. var data = that.data,
  150. itemsPerPage = that.pageTo < that.options.data.length ? that.options.data.length : that.pageTo,
  151. isColumnSearchableViaSelect = function (column) {
  152. return column.filterControl && column.filterControl.toLowerCase() === 'select' && column.searchable;
  153. },
  154. isFilterDataNotGiven = function (column) {
  155. return column.filterData === undefined || column.filterData.toLowerCase() === 'column';
  156. },
  157. hasSelectControlElement = function (selectControl) {
  158. return selectControl && selectControl.length > 0;
  159. };
  160. var z = that.options.pagination ?
  161. (that.options.sidePagination === 'server' ? that.pageTo : that.options.totalRows) :
  162. that.pageTo;
  163. $.each(that.header.fields, function (j, field) {
  164. var column = that.columns[that.fieldsColumnsIndex[field]],
  165. selectControl = $('.bootstrap-table-filter-control-' + escapeID(column.field));
  166. if (isColumnSearchableViaSelect(column) && isFilterDataNotGiven(column) && hasSelectControlElement(selectControl)) {
  167. if (selectControl.get(selectControl.length - 1).options.length === 0) {
  168. //Added the default option
  169. addOptionToSelectControl(selectControl, '', '');
  170. }
  171. var uniqueValues = {};
  172. for (var i = 0; i < z; i++) {
  173. //Added a new value
  174. var fieldValue = data[i][field],
  175. formattedValue = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, that.header.formatters[j], [fieldValue, data[i], i], fieldValue);
  176. uniqueValues[formattedValue] = fieldValue;
  177. }
  178. for (var key in uniqueValues) {
  179. addOptionToSelectControl(selectControl, uniqueValues[key], key);
  180. }
  181. sortSelectControl(selectControl);
  182. if (that.options.hideUnusedSelectOptions) {
  183. hideUnusedSelectOptions(selectControl, uniqueValues);
  184. }
  185. }
  186. });
  187. };
  188. var escapeID = function(id) {
  189. return String(id).replace( /(:|\.|\[|\]|,)/g, "\\$1" );
  190. };
  191. var createControls = function (that, header) {
  192. var addedFilterControl = false,
  193. isVisible,
  194. html;
  195. $.each(that.columns, function (i, column) {
  196. isVisible = 'hidden';
  197. html = [];
  198. if (!column.visible) {
  199. return;
  200. }
  201. if (!column.filterControl) {
  202. html.push('<div class="no-filter-control"></div>');
  203. } else {
  204. html.push('<div class="filter-control">');
  205. var nameControl = column.filterControl.toLowerCase();
  206. if (column.searchable && that.options.filterTemplate[nameControl]) {
  207. addedFilterControl = true;
  208. isVisible = 'visible';
  209. html.push(that.options.filterTemplate[nameControl](that, column.field, isVisible, column.filterControlPlaceholder ? column.filterControlPlaceholder : "", "filter-control-" + i));
  210. }
  211. }
  212. $.each(header.children().children(), function (i, tr) {
  213. tr = $(tr);
  214. if (tr.data('field') === column.field) {
  215. tr.find('.fht-cell').append(html.join(''));
  216. return false;
  217. }
  218. });
  219. if (column.filterData !== undefined && column.filterData.toLowerCase() !== 'column') {
  220. var filterDataType = getFilterDataMethod(filterDataMethods, column.filterData.substring(0, column.filterData.indexOf(':')));
  221. var filterDataSource, selectControl;
  222. if (filterDataType !== null) {
  223. filterDataSource = column.filterData.substring(column.filterData.indexOf(':') + 1, column.filterData.length);
  224. selectControl = $('.bootstrap-table-filter-control-' + escapeID(column.field));
  225. addOptionToSelectControl(selectControl, '', '');
  226. filterDataType(filterDataSource, selectControl);
  227. } else {
  228. throw new SyntaxError('Error. You should use any of these allowed filter data methods: var, json, url.' + ' Use like this: var: {key: "value"}');
  229. }
  230. var variableValues, key;
  231. switch (filterDataType) {
  232. case 'url':
  233. $.ajax({
  234. url: filterDataSource,
  235. dataType: 'json',
  236. success: function (data) {
  237. for (var key in data) {
  238. addOptionToSelectControl(selectControl, key, data[key]);
  239. }
  240. sortSelectControl(selectControl);
  241. }
  242. });
  243. break;
  244. case 'var':
  245. variableValues = window[filterDataSource];
  246. for (key in variableValues) {
  247. addOptionToSelectControl(selectControl, key, variableValues[key]);
  248. }
  249. sortSelectControl(selectControl);
  250. break;
  251. case 'jso':
  252. variableValues = JSON.parse(filterDataSource);
  253. for (key in variableValues) {
  254. addOptionToSelectControl(selectControl, key, variableValues[key]);
  255. }
  256. sortSelectControl(selectControl);
  257. break;
  258. }
  259. }
  260. });
  261. if (addedFilterControl) {
  262. header.off('keyup', 'input').on('keyup', 'input', function (event) {
  263. if (that.options.searchOnEnterKey && event.keyCode !== 13) {
  264. return;
  265. }
  266. if ($.inArray(event.keyCode, [37, 38, 39, 40]) > -1) {
  267. return;
  268. }
  269. clearTimeout(event.currentTarget.timeoutId || 0);
  270. event.currentTarget.timeoutId = setTimeout(function () {
  271. that.onColumnSearch(event);
  272. }, that.options.searchTimeOut);
  273. });
  274. header.off('change', 'select').on('change', 'select', function (event) {
  275. if (that.options.searchOnEnterKey && event.keyCode !== 13) {
  276. return;
  277. }
  278. if ($.inArray(event.keyCode, [37, 38, 39, 40]) > -1) {
  279. return;
  280. }
  281. clearTimeout(event.currentTarget.timeoutId || 0);
  282. event.currentTarget.timeoutId = setTimeout(function () {
  283. that.onColumnSearch(event);
  284. }, that.options.searchTimeOut);
  285. });
  286. header.off('mouseup', 'input').on('mouseup', 'input', function (event) {
  287. var $input = $(this),
  288. oldValue = $input.val();
  289. if (oldValue === "") {
  290. return;
  291. }
  292. setTimeout(function(){
  293. var newValue = $input.val();
  294. if (newValue === "") {
  295. clearTimeout(event.currentTarget.timeoutId || 0);
  296. event.currentTarget.timeoutId = setTimeout(function () {
  297. that.onColumnSearch(event);
  298. }, that.options.searchTimeOut);
  299. }
  300. }, 1);
  301. });
  302. if (header.find('.date-filter-control').length > 0) {
  303. $.each(that.columns, function (i, column) {
  304. if (column.filterControl !== undefined && column.filterControl.toLowerCase() === 'datepicker') {
  305. header.find('.date-filter-control.bootstrap-table-filter-control-' + column.field).datepicker(column.filterDatepickerOptions)
  306. .on('changeDate', function (e) {
  307. $(sprintf("#%s", e.currentTarget.id)).val(e.currentTarget.value);
  308. //Fired the keyup event
  309. $(e.currentTarget).keyup();
  310. });
  311. }
  312. });
  313. }
  314. } else {
  315. header.find('.filterControl').hide();
  316. }
  317. };
  318. var getDirectionOfSelectOptions = function (alignment) {
  319. alignment = alignment === undefined ? 'left' : alignment.toLowerCase();
  320. switch (alignment) {
  321. case 'left':
  322. return 'ltr';
  323. case 'right':
  324. return 'rtl';
  325. case 'auto':
  326. return 'auto';
  327. default:
  328. return 'ltr';
  329. }
  330. };
  331. var filterDataMethods =
  332. {
  333. 'var': function (filterDataSource, selectControl) {
  334. var variableValues = window[filterDataSource];
  335. for (var key in variableValues) {
  336. addOptionToSelectControl(selectControl, key, variableValues[key]);
  337. }
  338. sortSelectControl(selectControl);
  339. },
  340. 'url': function (filterDataSource, selectControl) {
  341. $.ajax({
  342. url: filterDataSource,
  343. dataType: 'json',
  344. success: function (data) {
  345. for (var key in data) {
  346. addOptionToSelectControl(selectControl, key, data[key]);
  347. }
  348. sortSelectControl(selectControl);
  349. }
  350. });
  351. },
  352. 'json':function (filterDataSource, selectControl) {
  353. var variableValues = JSON.parse(filterDataSource);
  354. for (var key in variableValues) {
  355. addOptionToSelectControl(selectControl, key, variableValues[key]);
  356. }
  357. sortSelectControl(selectControl);
  358. }
  359. };
  360. var getFilterDataMethod = function (objFilterDataMethod, searchTerm) {
  361. var keys = Object.keys(objFilterDataMethod);
  362. for (var i = 0; i < keys.length; i++) {
  363. if (keys[i] === searchTerm) {
  364. return objFilterDataMethod[searchTerm];
  365. }
  366. }
  367. return null;
  368. };
  369. $.extend($.fn.bootstrapTable.defaults, {
  370. filterControl: false,
  371. onColumnSearch: function (field, text) {
  372. return false;
  373. },
  374. filterShowClear: false,
  375. alignmentSelectControlOptions: undefined,
  376. filterTemplate: {
  377. input: function (that, field, isVisible, placeholder) {
  378. return sprintf('<input type="text" class="form-control bootstrap-table-filter-control-%s" style="width: 100%; visibility: %s" placeholder="%s">', field, isVisible, placeholder);
  379. },
  380. select: function (that, field, isVisible) {
  381. return sprintf('<select class="form-control bootstrap-table-filter-control-%s" style="width: 100%; visibility: %s" dir="%s"></select>',
  382. field, isVisible, getDirectionOfSelectOptions(that.options.alignmentSelectControlOptions));
  383. },
  384. datepicker: function (that, field, isVisible) {
  385. return sprintf('<input type="text" class="form-control date-filter-control bootstrap-table-filter-control-%s" style="width: 100%; visibility: %s">', field, isVisible);
  386. }
  387. },
  388. disableControlWhenSearch: false,
  389. searchOnEnterKey: false,
  390. //internal variables
  391. valuesFilterControl: []
  392. });
  393. $.extend($.fn.bootstrapTable.columnDefaults, {
  394. filterControl: undefined,
  395. filterData: undefined,
  396. filterDatepickerOptions: undefined,
  397. filterStrictSearch: false,
  398. filterStartsWithSearch: false,
  399. filterControlPlaceholder: ""
  400. });
  401. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  402. 'column-search.bs.table': 'onColumnSearch'
  403. });
  404. $.extend($.fn.bootstrapTable.defaults.icons, {
  405. clear: 'glyphicon-trash icon-clear'
  406. });
  407. $.extend($.fn.bootstrapTable.locales, {
  408. formatClearFilters: function () {
  409. return 'Clear Filters';
  410. }
  411. });
  412. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  413. $.fn.bootstrapTable.methods.push('triggerSearch');
  414. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  415. _init = BootstrapTable.prototype.init,
  416. _initToolbar = BootstrapTable.prototype.initToolbar,
  417. _initHeader = BootstrapTable.prototype.initHeader,
  418. _initBody = BootstrapTable.prototype.initBody,
  419. _initSearch = BootstrapTable.prototype.initSearch;
  420. BootstrapTable.prototype.init = function () {
  421. //Make sure that the filterControl option is set
  422. if (this.options.filterControl) {
  423. var that = this;
  424. // Compatibility: IE < 9 and old browsers
  425. if (!Object.keys) {
  426. objectKeys();
  427. }
  428. //Make sure that the internal variables are set correctly
  429. this.options.valuesFilterControl = [];
  430. this.$el.on('reset-view.bs.table', function () {
  431. //Create controls on $tableHeader if the height is set
  432. if (!that.options.height) {
  433. return;
  434. }
  435. //Avoid recreate the controls
  436. if (that.$tableHeader.find('select').length > 0 || that.$tableHeader.find('input').length > 0) {
  437. return;
  438. }
  439. createControls(that, that.$tableHeader);
  440. }).on('post-header.bs.table', function () {
  441. setValues(that);
  442. }).on('post-body.bs.table', function () {
  443. if (that.options.height) {
  444. fixHeaderCSS(that);
  445. }
  446. }).on('column-switch.bs.table', function() {
  447. setValues(that);
  448. }).on('load-success.bs.table', function() {
  449. that.EnableControls(true);
  450. }).on('load-error.bs.table', function() {
  451. that.EnableControls(true);
  452. });
  453. }
  454. _init.apply(this, Array.prototype.slice.apply(arguments));
  455. };
  456. BootstrapTable.prototype.initToolbar = function () {
  457. this.showToolbar = this.showToolbar || this.options.filterControl && this.options.filterShowClear;
  458. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  459. if (this.options.filterControl && this.options.filterShowClear) {
  460. var $btnGroup = this.$toolbar.find('>.btn-group'),
  461. $btnClear = $btnGroup.find('.filter-show-clear');
  462. if (!$btnClear.length) {
  463. $btnClear = $([
  464. sprintf('<button class="btn btn-%s filter-show-clear" ', this.options.buttonsClass),
  465. sprintf('type="button" title="%s">', this.options.formatClearFilters()),
  466. sprintf('<i class="%s %s"></i> ', this.options.iconsPrefix, this.options.icons.clear),
  467. '</button>'
  468. ].join('')).appendTo($btnGroup);
  469. $btnClear.off('click').on('click', $.proxy(this.clearFilterControl, this));
  470. }
  471. }
  472. };
  473. BootstrapTable.prototype.initHeader = function () {
  474. _initHeader.apply(this, Array.prototype.slice.apply(arguments));
  475. if (!this.options.filterControl) {
  476. return;
  477. }
  478. createControls(this, this.$header);
  479. };
  480. BootstrapTable.prototype.initBody = function () {
  481. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  482. initFilterSelectControls(this);
  483. };
  484. BootstrapTable.prototype.initSearch = function () {
  485. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  486. if (this.options.sidePagination === 'server') {
  487. return;
  488. }
  489. var that = this;
  490. var fp = $.isEmptyObject(that.filterColumnsPartial) ? null : that.filterColumnsPartial;
  491. //Check partial column filter
  492. that.data = fp ? $.grep(that.data, function (item, i) {
  493. for (var key in fp) {
  494. var thisColumn = that.columns[that.fieldsColumnsIndex[key]];
  495. var fval = fp[key].toLowerCase();
  496. var value = item[key];
  497. // Fix #142: search use formated data
  498. if (thisColumn && thisColumn.searchFormatter) {
  499. value = $.fn.bootstrapTable.utils.calculateObjectValue(that.header,
  500. that.header.formatters[$.inArray(key, that.header.fields)],
  501. [value, item, i], value);
  502. }
  503. if($.inArray(key, that.header.fields) !== -1 ) {
  504. if(typeof value === 'string' || typeof value === 'number') {
  505. if (thisColumn.filterStrictSearch) {
  506. if(value.toString().toLowerCase() === fval.toString().toLowerCase()) {
  507. return true;
  508. }
  509. } else if (thisColumn.filterStartsWithSearch) {
  510. if((value + '').toLowerCase().indexOf(fval) === 0) {
  511. return true;
  512. }
  513. } else {
  514. if((value + '').toLowerCase().indexOf(fval) !== -1) {
  515. return true;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. return false;
  522. }) : that.data;
  523. };
  524. BootstrapTable.prototype.initColumnSearch = function(filterColumnsDefaults) {
  525. copyValues(this);
  526. if (filterColumnsDefaults) {
  527. this.filterColumnsPartial = filterColumnsDefaults;
  528. this.updatePagination();
  529. for (var filter in filterColumnsDefaults) {
  530. this.trigger('column-search', filter, filterColumnsDefaults[filter]);
  531. }
  532. }
  533. };
  534. BootstrapTable.prototype.onColumnSearch = function (event) {
  535. if ($.inArray(event.keyCode, [37, 38, 39, 40]) > -1) {
  536. return;
  537. }
  538. copyValues(this);
  539. var text = $.trim($(event.currentTarget).val());
  540. var $field = $(event.currentTarget).closest('[data-field]').data('field');
  541. if ($.isEmptyObject(this.filterColumnsPartial)) {
  542. this.filterColumnsPartial = {};
  543. }
  544. if (text) {
  545. this.filterColumnsPartial[$field] = text;
  546. } else {
  547. delete this.filterColumnsPartial[$field];
  548. }
  549. // if the searchText is the same as the previously selected column value,
  550. // bootstrapTable will not try searching again (even though the selected column
  551. // may be different from the previous search). As a work around
  552. // we're manually appending some text to bootrap's searchText field
  553. // to guarantee that it will perform a search again when we call this.onSearch(event)
  554. this.searchText += "randomText";
  555. this.options.pageNumber = 1;
  556. this.EnableControls(false);
  557. this.onSearch(event);
  558. this.trigger('column-search', $field, text);
  559. };
  560. BootstrapTable.prototype.clearFilterControl = function () {
  561. if (this.options.filterControl && this.options.filterShowClear) {
  562. var that = this,
  563. cookies = collectBootstrapCookies(),
  564. header = getCurrentHeader(that),
  565. table = header.closest('table'),
  566. controls = header.find(getCurrentSearchControls(that)),
  567. search = that.$toolbar.find('.search input'),
  568. timeoutId = 0;
  569. $.each(that.options.valuesFilterControl, function (i, item) {
  570. item.value = '';
  571. });
  572. setValues(that);
  573. // Clear each type of filter if it exists.
  574. // Requires the body to reload each time a type of filter is found because we never know
  575. // which ones are going to be present.
  576. if (controls.length > 0) {
  577. this.filterColumnsPartial = {};
  578. $(controls[0]).trigger(controls[0].tagName === 'INPUT' ? 'keyup' : 'change');
  579. } else {
  580. return;
  581. }
  582. if (search.length > 0) {
  583. that.resetSearch();
  584. }
  585. // use the default sort order if it exists. do nothing if it does not
  586. if (that.options.sortName !== table.data('sortName') || that.options.sortOrder !== table.data('sortOrder')) {
  587. var sorter = header.find(sprintf('[data-field="%s"]', $(controls[0]).closest('table').data('sortName')));
  588. if (sorter.length > 0) {
  589. that.onSort(table.data('sortName'), table.data('sortName'));
  590. $(sorter).find('.sortable').trigger('click');
  591. }
  592. }
  593. // clear cookies once the filters are clean
  594. clearTimeout(timeoutId);
  595. timeoutId = setTimeout(function () {
  596. if (cookies && cookies.length > 0) {
  597. $.each(cookies, function (i, item) {
  598. if (that.deleteCookie !== undefined) {
  599. that.deleteCookie(item);
  600. }
  601. });
  602. }
  603. }, that.options.searchTimeOut);
  604. }
  605. };
  606. BootstrapTable.prototype.triggerSearch = function () {
  607. var header = getCurrentHeader(this),
  608. searchControls = getCurrentSearchControls(this);
  609. header.find(searchControls).each(function () {
  610. var el = $(this);
  611. if(el.is('select')) {
  612. el.change();
  613. } else {
  614. el.keyup();
  615. }
  616. });
  617. };
  618. BootstrapTable.prototype.EnableControls = function(enable) {
  619. if((this.options.disableControlWhenSearch) && (this.options.sidePagination === 'server')) {
  620. var header = getCurrentHeader(this),
  621. searchControls = getCurrentSearchControls(this);
  622. if(!enable) {
  623. header.find(searchControls).prop('disabled', 'disabled');
  624. } else {
  625. header.find(searchControls).removeProp('disabled');
  626. }
  627. }
  628. };
  629. })(jQuery);