Lines 1-90
Link Here
|
1 |
/* |
|
|
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 |
6 |
* |
7 |
* documentation at http://gregweber.info/projects/uitablefilter |
8 |
* |
9 |
* allows table rows to be filtered (made invisible) |
10 |
* <code> |
11 |
* t = $('table') |
12 |
* $.uiTableFilter( t, phrase ) |
13 |
* </code> |
14 |
* arguments: |
15 |
* jQuery object containing table rows |
16 |
* phrase to search for |
17 |
* optional arguments: |
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 |
20 |
*/ |
21 |
jQuery.uiTableFilter = function(jq, phrase, column, ifHidden){ |
22 |
var new_hidden = false; |
23 |
if( this.last_phrase === phrase ) return false; |
24 |
|
25 |
var phrase_length = phrase.length; |
26 |
var words = phrase.toLowerCase().split(" "); |
27 |
|
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() } |
32 |
|
33 |
if( column ) { |
34 |
var index = null; |
35 |
jq.find("thead > tr:last > th").each( function(i){ |
36 |
if( $.trim($(this).text()) == column ){ |
37 |
index = i; return false; |
38 |
} |
39 |
}); |
40 |
if( index == null ) throw("given column: " + column + " not found") |
41 |
|
42 |
getText = function(elem){ return jQuery(elem.find( |
43 |
("td:eq(" + index + ")") )).text() |
44 |
} |
45 |
} |
46 |
|
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) === |
50 |
this.last_phrase) ) { |
51 |
|
52 |
if( phrase[-1] === " " ) |
53 |
{ this.last_phrase = phrase; return false; } |
54 |
|
55 |
var words = words[-1]; // just search for the newest word |
56 |
|
57 |
// only hide visible rows |
58 |
matches = function(elem) {;} |
59 |
var elems = jq.find("tbody > tr:visible") |
60 |
} |
61 |
else { |
62 |
new_hidden = true; |
63 |
var elems = jq.find("tbody > tr") |
64 |
} |
65 |
|
66 |
elems.each(function(){ |
67 |
var elem = jQuery(this); |
68 |
jQuery.uiTableFilter.has_words( getText(elem), words, false ) ? |
69 |
matches(elem) : noMatch(elem); |
70 |
}); |
71 |
|
72 |
last_phrase = phrase; |
73 |
if( ifHidden && new_hidden ) ifHidden(); |
74 |
return jq; |
75 |
}; |
76 |
|
77 |
// caching for speedup |
78 |
jQuery.uiTableFilter.last_phrase = "" |
79 |
|
80 |
// not jQuery dependent |
81 |
// "" [""] -> Boolean |
82 |
// "" [""] Boolean -> Boolean |
83 |
jQuery.uiTableFilter.has_words = function( str, words, caseSensitive ) |
84 |
{ |
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; |
88 |
} |
89 |
return true; |
90 |
} |
91 |
- |