-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyfilter.js
113 lines (102 loc) · 3.1 KB
/
easyfilter.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Generated by CoffeeScript 1.9.3
(function() {
var hasInputBox, matchFilter, matchNumber, matchText, setupFilters, setupTable, updateFilters, updateTable, zip;
hasInputBox = function(elem) {
return $(elem).children('input').length > 0;
};
zip = function(a, b) {
var i, results;
i = 0;
results = [];
while (i < a.length) {
results.push([a[i], b[i]]);
i++;
}
return results;
};
setupFilters = function() {
return $('table[filterable-table]').each(function(i, table) {
return setupTable(table);
});
};
setupTable = function(t) {
$(t).find('thead').append('<tr filterable-column-inputs></tr>');
$(t).find('thead tr[filterable-header-row] th').each(function(i, elem) {
if ($(elem).is('[filterable-column]')) {
return $(t).find('thead tr[filterable-column-inputs]').append('<th><input filterable-logic=\'' + $(elem).attr('filterable-logic') + '\'></input></th>');
} else {
return $(t).find('thead tr[filterable-column-inputs]').append('<th></th>');
}
});
return $('table[filterable-table] thead tr th input').change(function() {
return updateFilters();
});
};
updateFilters = function() {
return $('table[filterable-table]').each(function(i, table) {
return updateTable(table);
});
};
updateTable = function(table) {
var cellsWithPatterns, patterns;
cellsWithPatterns = $(table).find('thead tr[filterable-column-inputs] th');
patterns = cellsWithPatterns.map(function(idx, cell) {
if (hasInputBox(cell)) {
return {
pattern: $(cell).children('input').val(),
logic: $(cell).children('input').attr('filterable-logic')
};
} else {
return {
pattern: null,
logic: null
};
}
});
patterns = patterns.toArray();
$(table).find('tbody tr').show();
return $(table).find('tbody tr').each(function(j, row) {
var filterMatches, i, results1, values;
values = $(row).find('td').map(function(i, x) {
return $(x).text();
});
filterMatches = zip(patterns, values).map(function(x) {
return matchFilter(x[0], x[1]);
});
i = 0;
results1 = [];
while (i < filterMatches.length) {
if (!filterMatches[i]) {
$(row).hide();
}
results1.push(i++);
}
return results1;
});
};
matchText = function(pattern, value) {
return value.toLowerCase().includes(pattern.toLowerCase());
};
matchNumber = function(pattern, value) {
value = value.replace(",", "");
if (/^\d/.test(pattern)) {
return value.includes(pattern);
}
return eval(value + pattern);
};
matchFilter = function(filter, value) {
var logic, pattern;
pattern = filter['pattern'];
logic = filter['logic'];
if ((logic === null) || (pattern.length === 0)) {
return true;
}
if (logic === 'text') {
return matchText(pattern, value);
} else if (logic === 'numeric') {
return matchNumber(pattern, value);
}
return true;
};
$(document).ready(setupFilters);
}).call(this);