Skip to content

Instantly share code, notes, and snippets.

@peterjwest
Last active September 24, 2015 01:28
Show Gist options
  • Save peterjwest/646886 to your computer and use it in GitHub Desktop.
Save peterjwest/646886 to your computer and use it in GitHub Desktop.
showFilter
(function($){
$.fn.showFilter = function(selectors, criteria, options) {
this.data("lastVal", this.val());
var id = 1;
while($(".showFilter"+id).length > 0) id++;
this.addClass(".showFilter"+id);
var input = this;
var filterInput = function(input) { return $.trim(input).toLowerCase().replace(/\s+/, " ") }
var test = function() {
var c = criteria($(this));
var filters = $(this).data("showFilter");
if (!filters) filters = $(this).data("showFilter", {}).data("showFilter");
for (i in filters) if (filters[i] && i !== id) return;
var terms = filterInput(input.val()).split(/\s+/);
var matched = false;
for (i in c) {
var matches = true;
for (j in terms) if (!(c[i].toLowerCase().indexOf(terms[j]) > -1)) matches = false;
if (matches) matched = true;
}
if (matched) {
filters[id] = false;
$(this).show();
}
else {
filters[id] = true;
$(this).hide();
}
}
var filter = function() {
if (filterInput($(this).val()) === filterInput($(this).data("lastVal"))) return;
alert();
$(this).data("lastVal", $(this).val());
$(selectors).each(test);
}
this.bind("keyup keydown input change paste", filter);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment