bootstrap-table-multiple-search.js 2.8 KB

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