bootstrap-table-multiple-search.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if (this.searchText === undefined) {
  16. return;
  17. }
  18. var strArray = this.searchText.split(" "),
  19. that = this,
  20. f = $.isEmptyObject(this.filterColumns) ? null : this.filterColumns,
  21. dataFiltered = [];
  22. if (strArray.length === 1) {
  23. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  24. } else {
  25. for (var i = 0; i < strArray.length; i++) {
  26. var str = strArray[i].trim();
  27. dataFiltered = str ? $.grep(dataFiltered.length === 0 ? this.options.data : dataFiltered, function (item, i) {
  28. for (var key in item) {
  29. key = $.isNumeric(key) ? parseInt(key, 10) : key;
  30. var value = item[key],
  31. column = that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, key)],
  32. j = $.inArray(key, that.header.fields);
  33. // Fix #142: search use formated data
  34. if (column && column.searchFormatter) {
  35. value = $.fn.bootstrapTable.utils.calculateObjectValue(column,
  36. that.header.formatters[j], [value, item, i], value);
  37. }
  38. var index = $.inArray(key, that.header.fields);
  39. if (index !== -1 && that.header.searchables[index] && (typeof value === 'string' || typeof value === 'number')) {
  40. if (that.options.strictSearch) {
  41. if ((value + '').toLowerCase() === str) {
  42. return true;
  43. }
  44. } else {
  45. if ((value + '').toLowerCase().indexOf(str) !== -1) {
  46. return true;
  47. }
  48. }
  49. }
  50. }
  51. return false;
  52. }) : this.data;
  53. }
  54. this.data = dataFiltered;
  55. }
  56. } else {
  57. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  58. }
  59. };
  60. }(jQuery);