forked from tommyp/multifilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multifilter.js
executable file
·67 lines (62 loc) · 2.17 KB
/
multifilter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(function($) {
"use strict";
$.fn.multifilter = function(options) {
var settings = $.extend( {
'target' : $('table'),
'method' : 'thead' // This can be thead or class
}, options);
jQuery.expr[":"].Equals = function(a, i, m) {
return (a.textContent || a.innerText || "").toUpperCase() === m[3].toUpperCase();
};
this.each(function() {
var $this = $(this);
var container = settings.target;
var row_tag = 'tr';
var item_tag = 'td';
var rows = container.find($(row_tag));
if (settings.method === 'thead') {
// Match the data-col attribute to the text in the thead
var col = container.find('th:Equals(' + $this.data('col') + ')');
var col_index = container.find($('thead th')).index(col);
};
if (settings.method === 'class') {
// Match the data-col attribute to the class on each column
var col = rows.first().find('td.' + $this.data('col'));
var col_index = rows.first().find('td').index(col);
};
$this.change(function() {
var filter = $this.val();
rows.each(function() {
var row = $(this);
var cell = $(row.children(item_tag)[col_index]);
if (filter) {
if (cell.text().toLowerCase().indexOf(filter.toLowerCase()) !== -1) {
cell.attr('data-filtered', 'positive');
} else {
cell.attr('data-filtered', 'negative');
}
if (row.find(item_tag + "[data-filtered=negative]").size() > 0) {
row.hide();
} else {
if (row.find(item_tag + "[data-filtered=positive]").size() > 0) {
row.show();
}
}
} else {
cell.attr('data-filtered', 'positive');
if (row.find(item_tag + "[data-filtered=negative]").size() > 0) {
row.hide();
} else {
if (row.find(item_tag + "[data-filtered=positive]").size() > 0) {
row.show();
}
}
}
});
return false;
}).keyup(function() {
$this.change();
});
});
};
})(jQuery);