-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexm-data-filter.html
107 lines (91 loc) · 2.45 KB
/
exm-data-filter.html
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
<link rel="import" href="../polymer/polymer.html">
<!--
`exm-data-filter` is filter element that lets you filter local data sets.
```html
<iron-ajax url="data/contacts.json" last-response="{{rawItems}}" auto></iron-ajax>
<exm-data-filter
raw-items="[[rawItems]]"
items="{{filteredItems}}"
filter-value="[[filterValue]]"
filter-fields="name,email">
</exm-data-filter>
<input is="iron-input" bind-value="{{filterValue}}">
<table is="exm-datatable">
<tbody is="exm-tbody" items='[[filteredItems]]'>
<template id="tbody">
<tr>
<td>[[item.name]]</td>
<td>[[item.email]]</td>
<td>[[item.phone]]</td>
</tr>
</template>
</tbody>
</table>
```
@element exm-data-filter
@demo demo/index.html
-->
<dom-module id="exm-data-filter">
<template></template>
<script>
Polymer({
is: 'exm-data-filter',
properties: {
/**
* Raw item list
*/
rawItems: {
type: Array
},
/**
* This array will conatin the filtered items list
*/
items: {
type: Array,
notify: true
},
/**
* Text string used for filtering the fields.
*/
filterValue: {
type: String
},
/**
* Comma seperated list of filter fields.
*/
filterFields: {
type: String
}
},
observers: [
'_updateItems(rawItems.*, filterValue, filterFields)'
],
_updateItems: function() {
/* Skip if raw items or filterfields are not set */
if(this.rawItems === undefined || this.filterFields === undefined) {
return;
}
/* return all items when filter is empty */
if(this.filterValue === undefined && this.filterValue === '') {
this.set('items', []);
this.set('items', this.rawItems);
return;
}
/* Look for occurances of the filter value in the item fields */
var items = [];
var filterFields = this.filterFields.split(',');
for(var i = 0; i < this.rawItems.length; i++) {
for(var j = 0; j < filterFields.length; j++) {
if(this.rawItems[i][filterFields[j].trim()] && this.rawItems[i][filterFields[j].trim()].toLowerCase()
.indexOf(this.filterValue.toLowerCase()) !== -1){
items.push(this.rawItems[i]);
break;
}
}
}
this.set('items', []);
this.set('items', items);
}
});
</script>
</dom-module>