-
Notifications
You must be signed in to change notification settings - Fork 0
/
slick.ext.headerRowFilter.js
118 lines (98 loc) · 3.01 KB
/
slick.ext.headerRowFilter.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
114
115
116
117
118
(function ($, undefined) {
$.extend(true, window, {
"Slick": {
"Ext": {
"Plugins": {
"HeaderRowFilter": HeaderRowFilter
}
}
}
});
function HeaderRowFilter(options) {
var grid,
self = this,
columnFilters = {},
dataView,
handler = new Slick.EventHandler(),
defaults = {
filter: defaultFilter,
columnFilters: columnFilters
},
$filterRow;
function init(g) {
grid = g;
dataView = grid.getData();
// if there is no data view then throw an error if my filter is used since it uses the data view
if (!(dataView instanceof Slick.Data.DataView) && (!options || !options.filter)) {
throw new Error("You must pass a filter if there is not data view attached to the grid");
}
if (options && options.filter && !options.columnFilters ) {
throw new Error("You must pass a column filter object in order to clear the filters properly");
}
options = $.extend(true, {}, defaults, options);
handler.subscribe(grid.onHeaderRowCellRendered, handleHeaderRowCellRendered)
// Force the grid to re-render the header now that the events are hooked up.
grid.setColumns(grid.getColumns());
$filterRow = $(grid.getHeaderRow());
// allows a filter to be set on the dataView from the text boxes in the header
$filterRow.delegate(":input", "change keyup", options.filter);
}
function destroy() {
handler.unsubscribeAll();
$(grid.getHeaderRow()).undelegate(":input", "change keyup", options.filter);
columnFilters = [];
}
function handleHeaderRowCellRendered(e, args) {
$(args.node).empty();
$("<input type='text'>")
.data("columnId", args.column.id)
.val(columnFilters[args.column.id])
.appendTo(args.node);
}
function defaultFilter(e) {
var columnId = $(this).data("columnId");
if (columnId != null) {
columnFilters[columnId] = $.trim($(this).val());
dataView.refresh();
}
dataView.setFilter(headerFilter);
dataView.refresh();
grid.resetActiveCell();
}
function headerFilter(item) {
for (var columnId in columnFilters) {
if (columnId !== undefined && columnFilters[columnId] !== "") {
var c = grid.getColumns()[grid.getColumnIndex(columnId)];
//if (item[c.field] != this.columnFilters[columnId]) {
// return false;
//}
if (item[c.field] !== null && columnFilters[columnId] !== null) {
if (item[c.field].toString().toLowerCase().indexOf(columnFilters[columnId].toString().toLowerCase()) === -1) {
return false;
}
}
else {
return false;
}
}
}
return true;
}
function clearFilters() {
// remove the text
$filterRow.find("input[type='text']").each(function () {
this.value = "";
});
// clear out column filters obj
for (var columnId in columnFilters) {
columnFilters[columnId] = "";
}
dataView.refresh();
}
$.extend(this, {
"init": init,
"destroy": destroy,
"clearFilters": clearFilters
});
}
})(jQuery);