JQuery to make a lazy filter (like iTunes)

| November 6, 2009 | Reply

This code is not mine, I’d link out to the source, but I’ve forgotten it. If you wrote this, thank you.

$(‘tbody tr’).hover(function(){
}, function(){

//default each row to visible
$(‘tbody tr’).addClass(‘visible’);

$(‘#filter’).keyup(function(event) {
//if esc is pressed or nothing is entered
if (event.keyCode == 27 || $(this).val() == ”) {
//if esc is pressed we want to clear the value of search box

//we want each row to be visible because if nothing
//is entered then all rows are matched.
$(‘tbody tr’).removeClass(‘visible’).show().addClass(‘visible’);
//if there is text, lets filter
else {
filter(‘tbody tr’, $(this).val());

//filter results based on query
function filter(selector, query) {
query =   $.trim(query); //trim white space
query = query.replace(/ /gi, ‘|’); //add OR for regex query

$(selector).each(function() {
($(this).text().search(new RegExp(query, “i”)) < 0) ? $(this).hide().removeClass(‘visible’) : $(this).show().addClass(‘visible’); }); }

Tags: ,

Category: Uncategorized

About the Author ()