jquery.quicksearch.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. (function($, window, document, undefined) {
  2. $.fn.quicksearch = function (target, opt) {
  3. var timeout, cache, rowcache, jq_results, val = '', e = this, options = $.extend({
  4. delay: 100,
  5. selector: null,
  6. stripeRows: null,
  7. loader: null,
  8. noResults: '',
  9. bind: 'keyup submit',
  10. onBefore: function () {
  11. return;
  12. },
  13. onAfter: function () {
  14. return;
  15. },
  16. show: function () {
  17. this.style.display = "";
  18. },
  19. hide: function () {
  20. this.style.display = "none";
  21. },
  22. prepareQuery: function (val) {
  23. return val.toLowerCase().split(' ');
  24. },
  25. testQuery: function (query, txt, _row) {
  26. for (var i = 0; i < query.length; i += 1) {
  27. if (txt.indexOf(query[i]) === -1) {
  28. return false;
  29. }
  30. }
  31. return true;
  32. }
  33. }, opt);
  34. this.go = function () {
  35. var i = 0,
  36. noresults = true,
  37. query = options.prepareQuery(val),
  38. val_empty = (val.replace(' ', '').length === 0);
  39. for (var i = 0, len = rowcache.length; i < len; i++) {
  40. if (val_empty || options.testQuery(query, cache[i], rowcache[i])) {
  41. options.show.apply(rowcache[i]);
  42. noresults = false;
  43. } else {
  44. options.hide.apply(rowcache[i]);
  45. }
  46. }
  47. if (noresults) {
  48. this.results(false);
  49. } else {
  50. this.results(true);
  51. this.stripe();
  52. }
  53. this.loader(false);
  54. options.onAfter();
  55. return this;
  56. };
  57. this.stripe = function () {
  58. if (typeof options.stripeRows === "object" && options.stripeRows !== null)
  59. {
  60. var joined = options.stripeRows.join(' ');
  61. var stripeRows_length = options.stripeRows.length;
  62. jq_results.not(':hidden').each(function (i) {
  63. $(this).removeClass(joined).addClass(options.stripeRows[i % stripeRows_length]);
  64. });
  65. }
  66. return this;
  67. };
  68. this.strip_html = function (input) {
  69. var output = input.replace(new RegExp('<[^<]+\>', 'g'), "");
  70. output = $.trim(output.toLowerCase());
  71. return output;
  72. };
  73. this.results = function (bool) {
  74. if (typeof options.noResults === "string" && options.noResults !== "") {
  75. if (bool) {
  76. $(options.noResults).hide();
  77. } else {
  78. $(options.noResults).show();
  79. }
  80. }
  81. return this;
  82. };
  83. this.loader = function (bool) {
  84. if (typeof options.loader === "string" && options.loader !== "") {
  85. (bool) ? $(options.loader).show() : $(options.loader).hide();
  86. }
  87. return this;
  88. };
  89. this.cache = function () {
  90. jq_results = $(target);
  91. if (typeof options.noResults === "string" && options.noResults !== "") {
  92. jq_results = jq_results.not(options.noResults);
  93. }
  94. var t = (typeof options.selector === "string") ? jq_results.find(options.selector) : $(target).not(options.noResults);
  95. cache = t.map(function () {
  96. return e.strip_html(this.innerHTML);
  97. });
  98. rowcache = jq_results.map(function () {
  99. return this;
  100. });
  101. return this.go();
  102. };
  103. this.trigger = function () {
  104. this.loader(true);
  105. options.onBefore();
  106. window.clearTimeout(timeout);
  107. timeout = window.setTimeout(function () {
  108. e.go();
  109. }, options.delay);
  110. return this;
  111. };
  112. this.cache();
  113. this.results(true);
  114. this.stripe();
  115. this.loader(false);
  116. return this.each(function () {
  117. $(this).bind(options.bind, function () {
  118. val = $(this).val();
  119. e.trigger();
  120. });
  121. });
  122. };
  123. }(jQuery, this, document));