listFilter jQuery plugin

February 13, 2009 at 3:59pm.

This entry is about Programming

5 comments.

A project I am working on needed a way to filter a long list of staff members. We needed to be able to filter by several different criteria, and several different interfaces (text search, dropdown lists). And it needed to happen in-place—no page reloads. Thus my listFilter jQuery plugin was born.

listFilter allows you to automatically filter out items from a list on the fly, using any number of criteria and data items. You can look at the example to see how it works.

How to use

First you need to inlude jQuery and the plugin script:

<script src=”/scripts/jquery.min.js”></script>
<script src=”/scripts/jquery.listfilter.js”></script>

You will need to things in you page: a list to filter and a form containing the controls you will use to set the filter criteria. These controls can be text inputs, textboxes, or select boxes. The list can be an actual list—<ul> or <ol>—or it can be any other element containing a group of similar elements (for instance a <table>, which contains <tr> elements). The trick here is that each filter criteria needs to match up with a sub-element of the list items. So if your list item looks like this: <li><h3 class=“name”>Name</h3> <p>Plumber</p></li>, you could have a criteria form that looks like this:

<form id=“criteria”>
  <label>Name:> <input type=“text” id=“name_filter” />
  <label>Job:> <input type=“text” id=“job_filter” />
</form>

You will notice that the id of each filter matches the class of an item in the list, with “_filter” added to the end. That is how the script knows what to filter.

Finally, you need to initialize the filtering with this line of code (either in the head of you document, or in an external script):

$(document).ready(function() {
$('#listToFilter').listFilter('#criteria');
});

There are three optional settings that you can pass to the listFilter function as a second parameter. listElement lets you specify the child element of #listToFilter that represents an individual item (default is “li”). transition specifies the effect used to show and hide the list items—can be “toggle”, “slide”, or “fade” (default is “toggle”). Finally, speed specifies the speed at which the transition will take place (default is 500). So if you wanted to filter the rows of a table by sliding unneeded rows up slowly, you would use:

$(document).ready(function() {
$('#tableToFilter').listFilter('#criteria', {listElement: 'tr', transition: 'slide', speed: 2000});
});

Download the script

Leave a comment if you have any questions or improvements. This plugin is loosely based on liveSearch by John Nunemaker as revised by John Resig.

Comments:

Conrad gravatar

Conrad on October 26, 2009 at 10:12am#1

Hey thanks for this script.

How well will it handle large amounts of weblog entries in an ee site for instance?

I’m thinking, listing archived news stories and using the search as a filter ?

EliVZ gravatar

EliVZ on October 26, 2009 at 10:59am#2

Conrad-
Since all the processing is done client-side, it will depend on the browser. I haven’t tried it will more than ~50 items, but since all of the processing is done on cached copies of the elements (rather than traversing the DOM repeatedly) it should be reasonably quick. If it starts to get bogged down, setting the speed to 0 will disable the graphical transitions, which will do a lot to speed things up (those transitions take quite a bit of processing power!).

Let me know how it goes!

Conrad gravatar

Conrad on October 28, 2009 at 4:35am#3

Cheers mate I will let you know how it goes. There may be usability issues with paging when you have many entries but for small weblogs it should work

Vincent Roman gravatar

Vincent Roman on December 2, 2009 at 11:43am#4

Hey Eli -

Great little plugin you have here.  Just wondering if it works with AJAXed content, i.e in instances where in JQuery you might use the LIVE function.

Thanks, Vincent -

Eli Van Zoeren gravatar

Eli Van Zoeren on December 3, 2009 at 10:17pm#5

Vincent-

Currently, the script wouldn’t take into account the updated list, although it could easily be updated to do so by moving the line that sets the rows variable inside the filter function (this would entail a slight performance hit, but probably not significant).

You could also simply call the script again in the callback from your AJAX function, if the code duplication doesn’t bother you.

Got something to say?