2 * Copyright (c) 2008 Greg Weber greg at gregweber.info
3 * Dual licensed under the MIT and GPL licenses:
4 * http://www.opensource.org/licenses/mit-license.php
5 * http://www.gnu.org/licenses/gpl.html
7 * documentation at http://gregweber.info/projects/uitablefilter
9 * allows table rows to be filtered (made invisible)
12 * $.uiTableFilter( t, phrase )
15 * jQuery object containing table rows
16 * phrase to search for
18 * column to limit search too (the column title in the table header)
19 * ifHidden - callback to execute if one or more elements was hidden
21 jQuery.uiTableFilter = function(jq, phrase, column, ifHidden){
22 var new_hidden = false;
23 if( this.last_phrase === phrase ) return false;
25 var phrase_length = phrase.length;
26 var words = phrase.toLowerCase().split(" ");
28 // these function pointers may change
29 var matches = function(elem) { elem.show() }
30 var noMatch = function(elem) { elem.hide(); new_hidden = true }
31 var getText = function(elem) { return elem.text() }
35 jq.find("thead > tr:last > th").each( function(i){
36 if( $.trim($(this).text()) == column ){
37 index = i; return false;
40 if( index == null ) throw("given column: " + column + " not found")
42 getText = function(elem){ return jQuery(elem.find(
43 ("td:eq(" + index + ")") )).text()
47 // if added one letter to last time,
48 // just check newest word and only need to hide
49 if( (words.size > 1) && (phrase.substr(0, phrase_length - 1) ===
52 if( phrase[-1] === " " )
53 { this.last_phrase = phrase; return false; }
55 var words = words[-1]; // just search for the newest word
57 // only hide visible rows
58 matches = function(elem) {;}
59 var elems = jq.find("tbody > tr:visible")
63 var elems = jq.find("tbody > tr")
66 elems.each(function(){
67 var elem = jQuery(this);
68 jQuery.uiTableFilter.has_words( getText(elem), words, false ) ?
69 matches(elem) : noMatch(elem);
73 if( ifHidden && new_hidden ) ifHidden();
77 // caching for speedup
78 jQuery.uiTableFilter.last_phrase = ""
80 // not jQuery dependent
82 // "" [""] Boolean -> Boolean
83 jQuery.uiTableFilter.has_words = function( str, words, caseSensitive )
85 var text = caseSensitive ? str : str.toLowerCase();
86 for (var i=0; i < words.length; i++) {
87 if (text.indexOf(words[i]) === -1) return false;