bootstrap-table-filter-control.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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.bootstrapTableFilterControl = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. function _classCallCheck(instance, Constructor) {
  16. if (!(instance instanceof Constructor)) {
  17. throw new TypeError("Cannot call a class as a function");
  18. }
  19. }
  20. var _createClass = function () {
  21. function defineProperties(target, props) {
  22. for (var i = 0; i < props.length; i++) {
  23. var descriptor = props[i];
  24. descriptor.enumerable = descriptor.enumerable || false;
  25. descriptor.configurable = true;
  26. if ("value" in descriptor) descriptor.writable = true;
  27. Object.defineProperty(target, descriptor.key, descriptor);
  28. }
  29. }
  30. return function (Constructor, protoProps, staticProps) {
  31. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  32. if (staticProps) defineProperties(Constructor, staticProps);
  33. return Constructor;
  34. };
  35. }();
  36. function _possibleConstructorReturn(self, call) {
  37. if (!self) {
  38. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  39. }
  40. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  41. }
  42. var _get = function get(object, property, receiver) {
  43. if (object === null) object = Function.prototype;
  44. var desc = Object.getOwnPropertyDescriptor(object, property);
  45. if (desc === undefined) {
  46. var parent = Object.getPrototypeOf(object);
  47. if (parent === null) {
  48. return undefined;
  49. } else {
  50. return get(parent, property, receiver);
  51. }
  52. } else if ("value" in desc) {
  53. return desc.value;
  54. } else {
  55. var getter = desc.get;
  56. if (getter === undefined) {
  57. return undefined;
  58. }
  59. return getter.call(receiver);
  60. }
  61. };
  62. function _inherits(subClass, superClass) {
  63. if (typeof superClass !== "function" && superClass !== null) {
  64. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  65. }
  66. subClass.prototype = Object.create(superClass && superClass.prototype, {
  67. constructor: {
  68. value: subClass,
  69. enumerable: false,
  70. writable: true,
  71. configurable: true
  72. }
  73. });
  74. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  75. }
  76. /**
  77. * @author: Dennis Hernández
  78. * @webSite: http://djhvscf.github.io/Blog
  79. * @version: v2.2.0
  80. */
  81. (function ($) {
  82. var Utils = $.fn.bootstrapTable.utils;
  83. var UtilsFilterControl = {
  84. getOptionsFromSelectControl: function getOptionsFromSelectControl(selectControl) {
  85. return selectControl.get(selectControl.length - 1).options;
  86. },
  87. hideUnusedSelectOptions: function hideUnusedSelectOptions(selectControl, uniqueValues) {
  88. var options = UtilsFilterControl.getOptionsFromSelectControl(selectControl);
  89. for (var i = 0; i < options.length; i++) {
  90. if (options[i].value !== '') {
  91. if (!uniqueValues.hasOwnProperty(options[i].value)) {
  92. selectControl.find(Utils.sprintf('option[value=\'%s\']', options[i].value)).hide();
  93. } else {
  94. selectControl.find(Utils.sprintf('option[value=\'%s\']', options[i].value)).show();
  95. }
  96. }
  97. }
  98. },
  99. addOptionToSelectControl: function addOptionToSelectControl(selectControl, _value, text) {
  100. var value = $.trim(_value);
  101. var $selectControl = $(selectControl.get(selectControl.length - 1));
  102. if (!UtilsFilterControl.existOptionInSelectControl(selectControl, value)) {
  103. $selectControl.append($('<option></option>').attr('value', value).text($('<div />').html(text).text()));
  104. }
  105. },
  106. sortSelectControl: function sortSelectControl(selectControl) {
  107. var $selectControl = $(selectControl.get(selectControl.length - 1));
  108. var $opts = $selectControl.find('option:gt(0)');
  109. $opts.sort(function (a, b) {
  110. var aa = $(a).text().toLowerCase();
  111. var bb = $(b).text().toLowerCase();
  112. if ($.isNumeric(a) && $.isNumeric(b)) {
  113. // Convert numerical values from string to float.
  114. aa = parseFloat(aa);
  115. bb = parseFloat(bb);
  116. }
  117. return aa > bb ? 1 : aa < bb ? -1 : 0;
  118. });
  119. $selectControl.find('option:gt(0)').remove();
  120. $selectControl.append($opts);
  121. },
  122. existOptionInSelectControl: function existOptionInSelectControl(selectControl, value) {
  123. var options = UtilsFilterControl.getOptionsFromSelectControl(selectControl);
  124. for (var i = 0; i < options.length; i++) {
  125. if (options[i].value === value.toString()) {
  126. // The value is not valid to add
  127. return true;
  128. }
  129. }
  130. // If we get here, the value is valid to add
  131. return false;
  132. },
  133. fixHeaderCSS: function fixHeaderCSS(_ref) {
  134. var $tableHeader = _ref.$tableHeader;
  135. $tableHeader.css('height', '77px');
  136. },
  137. getCurrentHeader: function getCurrentHeader(_ref2) {
  138. var $header = _ref2.$header,
  139. options = _ref2.options,
  140. $tableHeader = _ref2.$tableHeader;
  141. var header = $header;
  142. if (options.height) {
  143. header = $tableHeader;
  144. }
  145. return header;
  146. },
  147. getCurrentSearchControls: function getCurrentSearchControls(_ref3) {
  148. var options = _ref3.options;
  149. var searchControls = 'select, input';
  150. if (options.height) {
  151. searchControls = 'table select, table input';
  152. }
  153. return searchControls;
  154. },
  155. getCursorPosition: function getCursorPosition(el) {
  156. if (Utils.isIEBrowser()) {
  157. if ($(el).is('input[type=text]')) {
  158. var pos = 0;
  159. if ('selectionStart' in el) {
  160. pos = el.selectionStart;
  161. } else if ('selection' in document) {
  162. el.focus();
  163. var Sel = document.selection.createRange();
  164. var SelLength = document.selection.createRange().text.length;
  165. Sel.moveStart('character', -el.value.length);
  166. pos = Sel.text.length - SelLength;
  167. }
  168. return pos;
  169. }
  170. return -1;
  171. }
  172. return -1;
  173. },
  174. setCursorPosition: function setCursorPosition(el) {
  175. $(el).val(el.value);
  176. },
  177. copyValues: function copyValues(that) {
  178. var header = UtilsFilterControl.getCurrentHeader(that);
  179. var searchControls = UtilsFilterControl.getCurrentSearchControls(that);
  180. that.options.valuesFilterControl = [];
  181. header.find(searchControls).each(function () {
  182. that.options.valuesFilterControl.push({
  183. field: $(this).closest('[data-field]').data('field'),
  184. value: $(this).val(),
  185. position: UtilsFilterControl.getCursorPosition($(this).get(0)),
  186. hasFocus: $(this).is(':focus')
  187. });
  188. });
  189. },
  190. setValues: function setValues(that) {
  191. var field = null;
  192. var result = [];
  193. var header = UtilsFilterControl.getCurrentHeader(that);
  194. var searchControls = UtilsFilterControl.getCurrentSearchControls(that);
  195. if (that.options.valuesFilterControl.length > 0) {
  196. // Callback to apply after settings fields values
  197. var fieldToFocusCallback = null;
  198. header.find(searchControls).each(function (index, ele) {
  199. field = $(this).closest('[data-field]').data('field');
  200. result = $.grep(that.options.valuesFilterControl, function (valueObj) {
  201. return valueObj.field === field;
  202. });
  203. if (result.length > 0) {
  204. $(this).val(result[0].value);
  205. if (result[0].hasFocus) {
  206. // set callback if the field had the focus.
  207. fieldToFocusCallback = function (fieldToFocus, carretPosition) {
  208. // Closure here to capture the field and cursor position
  209. var closedCallback = function closedCallback() {
  210. fieldToFocus.focus();
  211. UtilsFilterControl.setCursorPosition(fieldToFocus, carretPosition);
  212. };
  213. return closedCallback;
  214. }($(this).get(0), result[0].position);
  215. }
  216. }
  217. });
  218. // Callback call.
  219. if (fieldToFocusCallback !== null) {
  220. fieldToFocusCallback();
  221. }
  222. }
  223. },
  224. collectBootstrapCookies: function collectBootstrapCookies() {
  225. var cookies = [];
  226. var foundCookies = document.cookie.match(/(?:bs.table.)(\w*)/g);
  227. if (foundCookies) {
  228. $.each(foundCookies, function (i, _cookie) {
  229. var cookie = _cookie;
  230. if (/./.test(cookie)) {
  231. cookie = cookie.split('.').pop();
  232. }
  233. if ($.inArray(cookie, cookies) === -1) {
  234. cookies.push(cookie);
  235. }
  236. });
  237. return cookies;
  238. }
  239. },
  240. escapeID: function escapeID(id) {
  241. return String(id).replace(/(:|\.|\[|\]|,)/g, '\\$1');
  242. },
  243. isColumnSearchableViaSelect: function isColumnSearchableViaSelect(_ref4) {
  244. var filterControl = _ref4.filterControl,
  245. searchable = _ref4.searchable;
  246. return filterControl && filterControl.toLowerCase() === 'select' && searchable;
  247. },
  248. isFilterDataNotGiven: function isFilterDataNotGiven(_ref5) {
  249. var filterData = _ref5.filterData;
  250. return filterData === undefined || filterData.toLowerCase() === 'column';
  251. },
  252. hasSelectControlElement: function hasSelectControlElement(selectControl) {
  253. return selectControl && selectControl.length > 0;
  254. },
  255. initFilterSelectControls: function initFilterSelectControls(that) {
  256. var data = that.data;
  257. var itemsPerPage = that.pageTo < that.options.data.length ? that.options.data.length : that.pageTo;
  258. var z = that.options.pagination ? that.options.sidePagination === 'server' ? that.pageTo : that.options.totalRows : that.pageTo;
  259. $.each(that.header.fields, function (j, field) {
  260. var column = that.columns[that.fieldsColumnsIndex[field]];
  261. var selectControl = $('.bootstrap-table-filter-control-' + UtilsFilterControl.escapeID(column.field));
  262. if (UtilsFilterControl.isColumnSearchableViaSelect(column) && UtilsFilterControl.isFilterDataNotGiven(column) && UtilsFilterControl.hasSelectControlElement(selectControl)) {
  263. if (selectControl.get(selectControl.length - 1).options.length === 0) {
  264. // Added the default option
  265. UtilsFilterControl.addOptionToSelectControl(selectControl, '', column.filterControlPlaceholder);
  266. }
  267. var uniqueValues = {};
  268. for (var i = 0; i < z; i++) {
  269. // Added a new value
  270. var fieldValue = data[i][field];
  271. var formattedValue = Utils.calculateObjectValue(that.header, that.header.formatters[j], [fieldValue, data[i], i], fieldValue);
  272. uniqueValues[formattedValue] = fieldValue;
  273. }
  274. // eslint-disable-next-line guard-for-in
  275. for (var key in uniqueValues) {
  276. UtilsFilterControl.addOptionToSelectControl(selectControl, uniqueValues[key], key);
  277. }
  278. UtilsFilterControl.sortSelectControl(selectControl);
  279. if (that.options.hideUnusedSelectOptions) {
  280. UtilsFilterControl.hideUnusedSelectOptions(selectControl, uniqueValues);
  281. }
  282. }
  283. });
  284. that.trigger('created-controls');
  285. },
  286. getFilterDataMethod: function getFilterDataMethod(objFilterDataMethod, searchTerm) {
  287. var keys = Object.keys(objFilterDataMethod);
  288. for (var i = 0; i < keys.length; i++) {
  289. if (keys[i] === searchTerm) {
  290. return objFilterDataMethod[searchTerm];
  291. }
  292. }
  293. return null;
  294. },
  295. createControls: function createControls(that, header) {
  296. var addedFilterControl = false;
  297. var isVisible = void 0;
  298. var html = void 0;
  299. $.each(that.columns, function (i, column) {
  300. isVisible = 'hidden';
  301. html = [];
  302. if (!column.visible) {
  303. return;
  304. }
  305. if (!column.filterControl) {
  306. html.push('<div class="no-filter-control"></div>');
  307. } else {
  308. html.push('<div class="filter-control">');
  309. var nameControl = column.filterControl.toLowerCase();
  310. if (column.searchable && that.options.filterTemplate[nameControl]) {
  311. addedFilterControl = true;
  312. isVisible = 'visible';
  313. html.push(that.options.filterTemplate[nameControl](that, column.field, isVisible, column.filterControlPlaceholder ? column.filterControlPlaceholder : '', 'filter-control-' + i));
  314. }
  315. }
  316. $.each(header.children().children(), function (i, tr) {
  317. var $tr = $(tr);
  318. if ($tr.data('field') === column.field) {
  319. $tr.find('.fht-cell').append(html.join(''));
  320. return false;
  321. }
  322. });
  323. if (column.filterData !== undefined && column.filterData.toLowerCase() !== 'column') {
  324. var filterDataType = UtilsFilterControl.getFilterDataMethod(
  325. /* eslint-disable no-use-before-define */
  326. filterDataMethods, column.filterData.substring(0, column.filterData.indexOf(':')));
  327. var filterDataSource = void 0;
  328. var selectControl = void 0;
  329. if (filterDataType !== null) {
  330. filterDataSource = column.filterData.substring(column.filterData.indexOf(':') + 1, column.filterData.length);
  331. selectControl = $('.bootstrap-table-filter-control-' + UtilsFilterControl.escapeID(column.field));
  332. UtilsFilterControl.addOptionToSelectControl(selectControl, '', column.filterControlPlaceholder);
  333. filterDataType(filterDataSource, selectControl);
  334. } else {
  335. throw new SyntaxError('Error. You should use any of these allowed filter data methods: var, json, url.' + ' Use like this: var: {key: "value"}');
  336. }
  337. var variableValues = void 0;
  338. var key = void 0;
  339. // eslint-disable-next-line default-case
  340. switch (filterDataType) {
  341. case 'url':
  342. $.ajax({
  343. url: filterDataSource,
  344. dataType: 'json',
  345. success: function success(data) {
  346. // eslint-disable-next-line guard-for-in
  347. for (var _key in data) {
  348. UtilsFilterControl.addOptionToSelectControl(selectControl, _key, data[_key]);
  349. }
  350. UtilsFilterControl.sortSelectControl(selectControl);
  351. }
  352. });
  353. break;
  354. case 'var':
  355. variableValues = window[filterDataSource];
  356. // eslint-disable-next-line guard-for-in
  357. for (key in variableValues) {
  358. UtilsFilterControl.addOptionToSelectControl(selectControl, key, variableValues[key]);
  359. }
  360. UtilsFilterControl.sortSelectControl(selectControl);
  361. break;
  362. case 'jso':
  363. variableValues = JSON.parse(filterDataSource);
  364. // eslint-disable-next-line guard-for-in
  365. for (key in variableValues) {
  366. UtilsFilterControl.addOptionToSelectControl(selectControl, key, variableValues[key]);
  367. }
  368. UtilsFilterControl.sortSelectControl(selectControl);
  369. break;
  370. }
  371. }
  372. });
  373. if (addedFilterControl) {
  374. header.off('keyup', 'input').on('keyup', 'input', function (event, obj) {
  375. // Simulate enter key action from clear button
  376. event.keyCode = obj ? obj.keyCode : event.keyCode;
  377. if (that.options.searchOnEnterKey && event.keyCode !== 13) {
  378. return;
  379. }
  380. if ($.inArray(event.keyCode, [37, 38, 39, 40]) > -1) {
  381. return;
  382. }
  383. var $currentTarget = $(event.currentTarget);
  384. if ($currentTarget.is(':checkbox') || $currentTarget.is(':radio')) {
  385. return;
  386. }
  387. clearTimeout(event.currentTarget.timeoutId || 0);
  388. event.currentTarget.timeoutId = setTimeout(function () {
  389. that.onColumnSearch(event);
  390. }, that.options.searchTimeOut);
  391. });
  392. header.off('change', 'select').on('change', 'select', function (event) {
  393. if (that.options.searchOnEnterKey && event.keyCode !== 13) {
  394. return;
  395. }
  396. if ($.inArray(event.keyCode, [37, 38, 39, 40]) > -1) {
  397. return;
  398. }
  399. clearTimeout(event.currentTarget.timeoutId || 0);
  400. event.currentTarget.timeoutId = setTimeout(function () {
  401. that.onColumnSearch(event);
  402. }, that.options.searchTimeOut);
  403. });
  404. header.off('mouseup', 'input').on('mouseup', 'input', function (event) {
  405. var $input = $(this);
  406. var oldValue = $input.val();
  407. if (oldValue === '') {
  408. return;
  409. }
  410. setTimeout(function () {
  411. var newValue = $input.val();
  412. if (newValue === '') {
  413. clearTimeout(event.currentTarget.timeoutId || 0);
  414. event.currentTarget.timeoutId = setTimeout(function () {
  415. that.onColumnSearch(event);
  416. }, that.options.searchTimeOut);
  417. }
  418. }, 1);
  419. });
  420. if (header.find('.date-filter-control').length > 0) {
  421. $.each(that.columns, function (i, _ref6) {
  422. var filterControl = _ref6.filterControl,
  423. field = _ref6.field,
  424. filterDatepickerOptions = _ref6.filterDatepickerOptions;
  425. if (filterControl !== undefined && filterControl.toLowerCase() === 'datepicker') {
  426. header.find('.date-filter-control.bootstrap-table-filter-control-' + field).datepicker(filterDatepickerOptions).on('changeDate', function (_ref7) {
  427. var currentTarget = _ref7.currentTarget;
  428. $(currentTarget).val(currentTarget.value);
  429. // Fired the keyup event
  430. $(currentTarget).keyup();
  431. });
  432. }
  433. });
  434. }
  435. } else {
  436. header.find('.filterControl').hide();
  437. }
  438. },
  439. getDirectionOfSelectOptions: function getDirectionOfSelectOptions(_alignment) {
  440. var alignment = _alignment === undefined ? 'left' : _alignment.toLowerCase();
  441. switch (alignment) {
  442. case 'left':
  443. return 'ltr';
  444. case 'right':
  445. return 'rtl';
  446. case 'auto':
  447. return 'auto';
  448. default:
  449. return 'ltr';
  450. }
  451. }
  452. };
  453. var filterDataMethods = {
  454. var: function _var(filterDataSource, selectControl) {
  455. var variableValues = window[filterDataSource];
  456. // eslint-disable-next-line guard-for-in
  457. for (var key in variableValues) {
  458. UtilsFilterControl.addOptionToSelectControl(selectControl, key, variableValues[key]);
  459. }
  460. UtilsFilterControl.sortSelectControl(selectControl);
  461. },
  462. url: function url(filterDataSource, selectControl) {
  463. $.ajax({
  464. url: filterDataSource,
  465. dataType: 'json',
  466. success: function success(data) {
  467. // eslint-disable-next-line guard-for-in
  468. for (var key in data) {
  469. UtilsFilterControl.addOptionToSelectControl(selectControl, key, data[key]);
  470. }
  471. UtilsFilterControl.sortSelectControl(selectControl);
  472. }
  473. });
  474. },
  475. json: function json(filterDataSource, selectControl) {
  476. var variableValues = JSON.parse(filterDataSource);
  477. // eslint-disable-next-line guard-for-in
  478. for (var key in variableValues) {
  479. UtilsFilterControl.addOptionToSelectControl(selectControl, key, variableValues[key]);
  480. }
  481. UtilsFilterControl.sortSelectControl(selectControl);
  482. }
  483. };
  484. var bootstrap = {
  485. 3: {
  486. icons: {
  487. clear: 'glyphicon-trash icon-clear'
  488. }
  489. },
  490. 4: {
  491. icons: {
  492. clear: 'fa-trash icon-clear'
  493. }
  494. }
  495. }[Utils.bootstrapVersion];
  496. $.extend($.fn.bootstrapTable.defaults, {
  497. filterControl: false,
  498. onColumnSearch: function onColumnSearch(field, text) {
  499. return false;
  500. },
  501. onCreatedControls: function onCreatedControls() {
  502. return true;
  503. },
  504. filterShowClear: false,
  505. alignmentSelectControlOptions: undefined,
  506. filterTemplate: {
  507. input: function input(that, field, isVisible, placeholder) {
  508. return Utils.sprintf('<input type="text" class="form-control bootstrap-table-filter-control-%s" style="width: 100%; visibility: %s" placeholder="%s">', field, isVisible, placeholder);
  509. },
  510. select: function select(_ref8, field, isVisible) {
  511. var options = _ref8.options;
  512. return Utils.sprintf('<select class="form-control bootstrap-table-filter-control-%s" style="width: 100%; visibility: %s" dir="%s"></select>', field, isVisible, UtilsFilterControl.getDirectionOfSelectOptions(options.alignmentSelectControlOptions));
  513. },
  514. datepicker: function datepicker(that, field, isVisible) {
  515. return Utils.sprintf('<input type="text" class="form-control date-filter-control bootstrap-table-filter-control-%s" style="width: 100%; visibility: %s">', field, isVisible);
  516. }
  517. },
  518. disableControlWhenSearch: false,
  519. searchOnEnterKey: false,
  520. // internal variables
  521. valuesFilterControl: []
  522. });
  523. $.extend($.fn.bootstrapTable.columnDefaults, {
  524. filterControl: undefined,
  525. filterData: undefined,
  526. filterDatepickerOptions: undefined,
  527. filterStrictSearch: false,
  528. filterStartsWithSearch: false,
  529. filterControlPlaceholder: ''
  530. });
  531. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  532. 'column-search.bs.table': 'onColumnSearch',
  533. 'created-controls.bs.table': 'onCreatedControls'
  534. });
  535. $.extend($.fn.bootstrapTable.defaults.icons, {
  536. clear: bootstrap.icons.clear
  537. });
  538. $.extend($.fn.bootstrapTable.locales, {
  539. formatClearFilters: function formatClearFilters() {
  540. return 'Clear Filters';
  541. }
  542. });
  543. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  544. $.fn.bootstrapTable.methods.push('triggerSearch');
  545. $.fn.bootstrapTable.methods.push('clearFilterControl');
  546. $.BootstrapTable = function (_$$BootstrapTable) {
  547. _inherits(_class, _$$BootstrapTable);
  548. function _class() {
  549. _classCallCheck(this, _class);
  550. return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
  551. }
  552. _createClass(_class, [{
  553. key: 'init',
  554. value: function init() {
  555. // Make sure that the filterControl option is set
  556. if (this.options.filterControl) {
  557. var that = this;
  558. // Make sure that the internal variables are set correctly
  559. this.options.valuesFilterControl = [];
  560. this.$el.on('reset-view.bs.table', function () {
  561. // Create controls on $tableHeader if the height is set
  562. if (!that.options.height) {
  563. return;
  564. }
  565. // Avoid recreate the controls
  566. if (that.$tableHeader.find('select').length > 0 || that.$tableHeader.find('input').length > 0) {
  567. return;
  568. }
  569. UtilsFilterControl.createControls(that, that.$tableHeader);
  570. }).on('post-header.bs.table', function () {
  571. UtilsFilterControl.setValues(that);
  572. }).on('post-body.bs.table', function () {
  573. if (that.options.height) {
  574. UtilsFilterControl.fixHeaderCSS(that);
  575. }
  576. }).on('column-switch.bs.table', function () {
  577. UtilsFilterControl.setValues(that);
  578. }).on('load-success.bs.table', function () {
  579. that.EnableControls(true);
  580. }).on('load-error.bs.table', function () {
  581. that.EnableControls(true);
  582. });
  583. }
  584. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'init', this).call(this);
  585. }
  586. }, {
  587. key: 'initToolbar',
  588. value: function initToolbar() {
  589. this.showToolbar = this.showToolbar || this.options.filterControl && this.options.filterShowClear;
  590. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initToolbar', this).call(this);
  591. if (this.options.filterControl && this.options.filterShowClear) {
  592. var $btnGroup = this.$toolbar.find('>.btn-group');
  593. var $btnClear = $btnGroup.find('.filter-show-clear');
  594. if (!$btnClear.length) {
  595. $btnClear = $([Utils.sprintf('<button class="btn btn-%s filter-show-clear" ', this.options.buttonsClass), Utils.sprintf('type="button" title="%s">', this.options.formatClearFilters()), Utils.sprintf('<i class="%s %s"></i> ', this.options.iconsPrefix, this.options.icons.clear), '</button>'].join('')).appendTo($btnGroup);
  596. $btnClear.off('click').on('click', $.proxy(this.clearFilterControl, this));
  597. }
  598. }
  599. }
  600. }, {
  601. key: 'initHeader',
  602. value: function initHeader() {
  603. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initHeader', this).call(this);
  604. if (!this.options.filterControl) {
  605. return;
  606. }
  607. UtilsFilterControl.createControls(this, this.$header);
  608. }
  609. }, {
  610. key: 'initBody',
  611. value: function initBody() {
  612. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initBody', this).call(this);
  613. UtilsFilterControl.initFilterSelectControls(this);
  614. }
  615. }, {
  616. key: 'initSearch',
  617. value: function initSearch() {
  618. var that = this;
  619. var fp = $.isEmptyObject(that.filterColumnsPartial) ? null : that.filterColumnsPartial;
  620. if (fp === null || Object.keys(fp).length <= 1) {
  621. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initSearch', this).call(this);
  622. }
  623. if (this.options.sidePagination === 'server') {
  624. return;
  625. }
  626. if (fp === null) {
  627. return;
  628. }
  629. // Check partial column filter
  630. that.data = fp ? that.options.data.filter(function (item, i) {
  631. var itemIsExpected = [];
  632. Object.keys(item).forEach(function (key, index) {
  633. var thisColumn = that.columns[that.fieldsColumnsIndex[key]];
  634. var fval = (fp[key] || '').toLowerCase();
  635. var value = item[key];
  636. if (fval === '') {
  637. itemIsExpected.push(true);
  638. } else {
  639. // Fix #142: search use formated data
  640. if (thisColumn && thisColumn.searchFormatter) {
  641. value = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, that.header.formatters[$.inArray(key, that.header.fields)], [value, item, i], value);
  642. }
  643. if ($.inArray(key, that.header.fields) !== -1) {
  644. if (typeof value === 'string' || typeof value === 'number') {
  645. if (thisColumn.filterStrictSearch) {
  646. if (value.toString().toLowerCase() === fval.toString().toLowerCase()) {
  647. itemIsExpected.push(true);
  648. } else {
  649. itemIsExpected.push(false);
  650. }
  651. } else if (thisColumn.filterStartsWithSearch) {
  652. if (('' + value).toLowerCase().indexOf(fval) === 0) {
  653. itemIsExpected.push(true);
  654. } else {
  655. itemIsExpected.push(false);
  656. }
  657. } else {
  658. if (('' + value).toLowerCase().includes(fval)) {
  659. itemIsExpected.push(true);
  660. } else {
  661. itemIsExpected.push(false);
  662. }
  663. }
  664. }
  665. }
  666. }
  667. });
  668. return !itemIsExpected.includes(false);
  669. }) : that.data;
  670. }
  671. }, {
  672. key: 'initColumnSearch',
  673. value: function initColumnSearch(filterColumnsDefaults) {
  674. UtilsFilterControl.copyValues(this);
  675. if (filterColumnsDefaults) {
  676. this.filterColumnsPartial = filterColumnsDefaults;
  677. this.updatePagination();
  678. // eslint-disable-next-line guard-for-in
  679. for (var filter in filterColumnsDefaults) {
  680. this.trigger('column-search', filter, filterColumnsDefaults[filter]);
  681. }
  682. }
  683. }
  684. }, {
  685. key: 'onColumnSearch',
  686. value: function onColumnSearch(event) {
  687. if ($.inArray(event.keyCode, [37, 38, 39, 40]) > -1) {
  688. return;
  689. }
  690. UtilsFilterControl.copyValues(this);
  691. var text = $.trim($(event.currentTarget).val());
  692. var $field = $(event.currentTarget).closest('[data-field]').data('field');
  693. if ($.isEmptyObject(this.filterColumnsPartial)) {
  694. this.filterColumnsPartial = {};
  695. }
  696. if (text) {
  697. this.filterColumnsPartial[$field] = text;
  698. } else {
  699. delete this.filterColumnsPartial[$field];
  700. }
  701. // if the searchText is the same as the previously selected column value,
  702. // bootstrapTable will not try searching again (even though the selected column
  703. // may be different from the previous search). As a work around
  704. // we're manually appending some text to bootrap's searchText field
  705. // to guarantee that it will perform a search again when we call this.onSearch(event)
  706. this.searchText += 'randomText';
  707. this.options.pageNumber = 1;
  708. this.EnableControls(false);
  709. this.onSearch(event);
  710. this.trigger('column-search', $field, text);
  711. }
  712. }, {
  713. key: 'clearFilterControl',
  714. value: function clearFilterControl() {
  715. if (this.options.filterControl && this.options.filterShowClear) {
  716. var that = this;
  717. var cookies = UtilsFilterControl.collectBootstrapCookies();
  718. var header = UtilsFilterControl.getCurrentHeader(that);
  719. var table = header.closest('table');
  720. var controls = header.find(UtilsFilterControl.getCurrentSearchControls(that));
  721. var search = that.$toolbar.find('.search input');
  722. var hasValues = false;
  723. var timeoutId = 0;
  724. $.each(that.options.valuesFilterControl, function (i, item) {
  725. hasValues = hasValues ? true : item.value !== '';
  726. item.value = '';
  727. });
  728. UtilsFilterControl.setValues(that);
  729. // clear cookies once the filters are clean
  730. clearTimeout(timeoutId);
  731. timeoutId = setTimeout(function () {
  732. if (cookies && cookies.length > 0) {
  733. $.each(cookies, function (i, item) {
  734. if (that.deleteCookie !== undefined) {
  735. that.deleteCookie(item);
  736. }
  737. });
  738. }
  739. }, that.options.searchTimeOut);
  740. // If there is not any value in the controls exit this method
  741. if (!hasValues) {
  742. return;
  743. }
  744. // Clear each type of filter if it exists.
  745. // Requires the body to reload each time a type of filter is found because we never know
  746. // which ones are going to be present.
  747. if (controls.length > 0) {
  748. this.filterColumnsPartial = {};
  749. $(controls[0]).trigger(controls[0].tagName === 'INPUT' ? 'keyup' : 'change', { keyCode: 13 });
  750. } else {
  751. return;
  752. }
  753. if (search.length > 0) {
  754. that.resetSearch();
  755. }
  756. // use the default sort order if it exists. do nothing if it does not
  757. if (that.options.sortName !== table.data('sortName') || that.options.sortOrder !== table.data('sortOrder')) {
  758. var sorter = header.find(Utils.sprintf('[data-field="%s"]', $(controls[0]).closest('table').data('sortName')));
  759. if (sorter.length > 0) {
  760. that.onSort({ type: 'keypress', currentTarget: sorter });
  761. $(sorter).find('.sortable').trigger('click');
  762. }
  763. }
  764. }
  765. }
  766. }, {
  767. key: 'triggerSearch',
  768. value: function triggerSearch() {
  769. var header = UtilsFilterControl.getCurrentHeader(this);
  770. var searchControls = UtilsFilterControl.getCurrentSearchControls(this);
  771. header.find(searchControls).each(function () {
  772. var el = $(this);
  773. if (el.is('select')) {
  774. el.change();
  775. } else {
  776. el.keyup();
  777. }
  778. });
  779. }
  780. }, {
  781. key: 'EnableControls',
  782. value: function EnableControls(enable) {
  783. if (this.options.disableControlWhenSearch && this.options.sidePagination === 'server') {
  784. var header = UtilsFilterControl.getCurrentHeader(this);
  785. var searchControls = UtilsFilterControl.getCurrentSearchControls(this);
  786. if (!enable) {
  787. header.find(searchControls).prop('disabled', 'disabled');
  788. } else {
  789. header.find(searchControls).removeProp('disabled');
  790. }
  791. }
  792. }
  793. }]);
  794. return _class;
  795. }($.BootstrapTable);
  796. })(jQuery);
  797. });