Skip to content

Commit fedf89b

Browse files
committed
Merge remote-tracking branch '@gregwebs/master'
2 parents 331c56e + 347a97c commit fedf89b

File tree

3 files changed

+76
-72
lines changed

3 files changed

+76
-72
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
# Original script
1+
# Repository
22

33
https://github.com/gregwebs/jquery-uitablefilter
4+
45
## What's new ?
56
With this version the 'column' argument has been modified to be an array of columns instead of being just one column.
67
Consequently, the script is now able to search in several columns. If you want the original behaviour back, you can put a simple String.
78
If you don't want to create any filter, just put an empty array as argument.
89

910
## Download
10-
Download the latest version here : https://raw.github.com/natinusala/jquery-uitablefilter/master/jquery.uitablefilter.js
11+
Download the latest version here : https://github.com/gregwebs/jquery-uitablefilter/blob/master/jquery.uitablefilter.js
1112

1213
## Install
1314

jquery.uitablefilter.js

+72-69
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
* Dual licensed under the MIT and GPLv2 licenses just as jQuery is:
44
* http://jquery.org/license
55
*
6-
* Multi-columns fork by natinusala
6+
* Multi-columns support by natinusala
77
*
88
* documentation at http://gregweber.info/projects/uitablefilter
9-
* https://github.com/natinusala/jquery-uitablefilter
109
*
1110
* allows table rows to be filtered (made invisible)
1211
* <code>
@@ -19,87 +18,91 @@
1918
* optional arguments:
2019
* array of columns to limit search too (the column title in the table header)
2120
* ifHidden - callback to execute if one or more elements was hidden
21+
* tdElem - specific element within <td> to be considered for searching or to limit search to,
22+
* default:whole <td>. useful if <td> has more than one elements inside but want to
23+
* limit search within only some of elements or only visible elements. eg tdElem can be "td span"
2224
*/
2325
(function ($) {
24-
$.uiTableFilter = function (jq, phrase, column, ifHidden) {
25-
var new_hidden = false;
26-
if (this.last_phrase === phrase) return false;
26+
$.uiTableFilter = function (jq, phrase, column, ifHidden, tdElem) {
27+
if (!tdElem) tdElem = "td";
28+
var new_hidden = false;
29+
if (this.last_phrase === phrase) return false;
2730

28-
var phrase_length = phrase.length;
29-
var words = phrase.toLowerCase().split(" ");
31+
var phrase_length = phrase.length;
32+
var words = phrase.toLowerCase().split(" ");
3033

31-
// these function pointers may change
32-
var matches = function (elem) { elem.show() }
33-
var noMatch = function (elem) { elem.hide(); new_hidden = true }
34-
var getText = function (elem) { return elem.text() }
34+
// these function pointers may change
35+
var matches = function (elem) { elem.show() }
36+
var noMatch = function (elem) { elem.hide(); new_hidden = true }
37+
var getText = function (elem) { return elem.text() }
3538

36-
if (column) {
37-
if (!$.isArray(column)) {
38-
column = new Array(column);
39-
}
39+
if (column) {
40+
if (!$.isArray(column)) {
41+
column = new Array(column);
42+
}
4043

41-
var index = new Array();
44+
var index = new Array();
4245

43-
jq.find("thead > tr:last > th").each(function (i) {
44-
for (var j = 0; j < column.length; j++) {
45-
if ($.trim($(this).text()) == column[j]) {
46-
index[j] = i;
47-
break;
48-
}
49-
}
46+
jq.find("thead > tr:last > th").each(function (i) {
47+
for (var j = 0; j < column.length; j++) {
48+
if ($.trim($(this).text()) == column[j]) {
49+
index[j] = i;
50+
break;
51+
}
52+
}
5053

51-
});
54+
});
5255

53-
getText = function (elem) {
54-
var selector = "";
55-
for (var i = 0; i < index.length; i++) {
56-
if (i != 0) { selector += ","; }
57-
selector += "td:eq(" + index[i] + ")";
58-
}
59-
return $(elem.find((selector))).text();
60-
}
61-
}
56+
getText = function (elem) {
57+
var selector = "";
58+
for (var i = 0; i < index.length; i++) {
59+
if (i != 0) { selector += ","; }
60+
selector += tdElem + ":eq(" + index[i] + ")";
61+
}
62+
return $(elem.find((selector))).text();
63+
}
64+
}
6265

63-
// if added one letter to last time,
64-
// just check newest word and only need to hide
65-
if ((words.size > 1) && (phrase.substr(0, phrase_length - 1) ===
66-
this.last_phrase)) {
66+
// if added one letter to last time,
67+
// just check newest word and only need to hide
68+
if ((words.size > 1) && (phrase.substr(0, phrase_length - 1) ===
69+
this.last_phrase)) {
6770

68-
if (phrase[-1] === " ") { this.last_phrase = phrase; return false; }
71+
if (phrase[-1] === " ") { this.last_phrase = phrase; return false; }
6972

70-
var words = words[-1]; // just search for the newest word
73+
var words = words[-1]; // just search for the newest word
7174

72-
// only hide visible rows
73-
matches = function (elem) { ; }
74-
var elems = jq.find("tbody:first > tr:visible")
75-
}
76-
else {
77-
new_hidden = true;
78-
var elems = jq.find("tbody:first > tr")
79-
}
75+
// only hide visible rows
76+
matches = function (elem) { ; }
77+
var elems = jq.find("tbody:first > tr:visible")
78+
}
79+
else {
80+
new_hidden = true;
81+
var elems = jq.find("tbody:first > tr")
82+
}
8083

81-
elems.each(function () {
82-
var elem = $(this);
83-
$.uiTableFilter.has_words(getText(elem), words, false) ?
84-
matches(elem) : noMatch(elem);
85-
});
84+
elems.each(function () {
85+
var elem = $(this);
86+
$.uiTableFilter.has_words(getText(elem), words, false) ?
87+
matches(elem) : noMatch(elem);
88+
});
8689

87-
last_phrase = phrase;
88-
if (ifHidden && new_hidden) ifHidden();
89-
return jq;
90-
};
90+
last_phrase = phrase;
91+
if (ifHidden && new_hidden) ifHidden();
92+
return jq;
93+
};
9194

92-
// caching for speedup
93-
$.uiTableFilter.last_phrase = ""
95+
// caching for speedup
96+
$.uiTableFilter.last_phrase = ""
9497

95-
// not jQuery dependent
96-
// "" [""] -> Boolean
97-
// "" [""] Boolean -> Boolean
98-
$.uiTableFilter.has_words = function (str, words, caseSensitive) {
99-
var text = caseSensitive ? str : str.toLowerCase();
100-
for (var i = 0; i < words.length; i++) {
101-
if (text.indexOf(words[i]) === -1) return false;
102-
}
103-
return true;
104-
}
105-
})(jQuery);
98+
// not jQuery dependent
99+
// "" [""] -> Boolean
100+
// "" [""] Boolean -> Boolean
101+
$.uiTableFilter.has_words = function (str, words, caseSensitive) {
102+
var text = caseSensitive ? str : str.toLowerCase();
103+
for (var i = 0; i < words.length; i++) {
104+
if (text.indexOf(words[i]) === -1) return false;
105+
}
106+
return true;
107+
}
108+
})(jQuery);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"description": "jQuery UI table filter",
55
"main": "jquery.uitablefilter.js",
6-
"repository": "https://github.com/natinusala/jquery-uitablefilter",
6+
"repository": "https://github.com/gregwebs/jquery-uitablefilter",
77
"author": {
88
"name": "Greg Weber",
99
"email": "[email protected]"

0 commit comments

Comments
 (0)