forked from stevenrskelton/sortable-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
columnFiltersMixin.js
59 lines (59 loc) · 1.98 KB
/
columnFiltersMixin.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
/**
* Column Filters
*/
var columnFiltersMixin = {
disableColumnFilters: false,
filterOps: [{title:'Equals',op:'='},{title:'Less Than',op:'<'},{title:'Greater Than',op:'>'}],
getFilterOps: function(column){
return this.filterOps.map(function(element){
if(typeof element === 'object') return {title:element.title||element.op,op:element.op,html:element.html||element.op};
else return {title:element,op:element,html:element};
});
},
forceFilterRefresh: 0,
generateColumnFilters: function(){
var filters = [];
this.columns.forEach(function(column){
if(column.filters) column.filters.forEach(function(filter){
filters.push({ column: column.name, op: filter.op, value: filter.value });
});
});
return filters;
},
createFilter: function(event,i,o){
event.stopPropagation();
var dropdown = o.querySelector('core-dropdown');
if(dropdown.style.display == 'none') dropdown.toggle();
},
addFilter: function(event,i,o){
var dropdown = o.parentElement;
var input = dropdown.querySelector('input[type="text"]');
if(input.value!==''){
var op = o.getAttribute('data-op');
var model = o.templateInstance.model;
if(!model.column.filters) model.column.filters = [];
model.column.filters.push({op: op, value: input.value});
dropdown.toggle();
input.value = '';
if(this.dataSource) this.dataSource.filters = this.generateColumnFilters();
this.forceFilterRefresh++;
}else dropdown.toggle();
},
removeFilter: function(e,i,o){
var model = o.templateInstance.model;
var index = model.column.filters.indexOf(model.filter);
model.column.filters.splice(index, 1);
if(this.columns.every(function(column){
return !column.filters || column.filters.length === 0;
})) this.forceFilterRefresh = 0;
if(this.dataSource) this.dataSource.filters = this.generateColumnFilters();
},
clearFilters: function(e,i,o){
if(confirm('Clear all filters?')){
this.columns.forEach(function(column){
column.filters = null;
});
this.forceFilterRefresh = 0;
}
}
}