bootstrap-table-multiple-search.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v1.0.0
  5. */
  6. !function ($) {
  7. 'use strict';
  8. $.extend($.fn.bootstrapTable.defaults, {
  9. multipleSearch: false
  10. });
  11. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  12. _initSearch = BootstrapTable.prototype.initSearch;
  13. BootstrapTable.prototype.initSearch = function () {
  14. if (this.options.multipleSearch) {
  15. var strArray = this.searchText.split(" "),
  16. that = this,
  17. f = $.isEmptyObject(this.filterColumns) ? null : this.filterColumns,
  18. dataFiltered = [];
  19. if (strArray.length === 1) {
  20. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  21. } else {
  22. for (var i = 0; i < strArray.length; i++) {
  23. var str = strArray[i].trim();
  24. dataFiltered = str ? $.grep(dataFiltered.length === 0 ? this.options.data : dataFiltered, function (item, i) {
  25. for (var key in item) {
  26. key = $.isNumeric(key) ? parseInt(key, 10) : key;
  27. var value = item[key],
  28. column = that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, key)],
  29. j = $.inArray(key, that.header.fields);
  30. // Fix #142: search use formated data
  31. if (column && column.searchFormatter) {
  32. value = $.fn.bootstrapTable.utils.calculateObjectValue(column,
  33. that.header.formatters[j], [value, item, i], value);
  34. }
  35. var index = $.inArray(key, that.header.fields);
  36. if (index !== -1 && that.header.searchables[index] && (typeof value === 'string' || typeof value === 'number')) {
  37. if (that.options.strictSearch) {
  38. if ((value + '').toLowerCase() === str) {
  39. return true;
  40. }
  41. } else {
  42. if ((value + '').toLowerCase().indexOf(str) !== -1) {
  43. return true;
  44. }
  45. }
  46. }
  47. }
  48. return false;
  49. }) : this.data;
  50. }
  51. this.data = dataFiltered;
  52. }
  53. } else {
  54. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  55. }
  56. };
  57. }(jQuery);