-
Notifications
You must be signed in to change notification settings - Fork 2
/
VgtFilterRow.vue
218 lines (192 loc) · 6.44 KB
/
VgtFilterRow.vue
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<template>
<tr v-if="hasFilterRow">
<th v-if="lineNumbers"></th>
<th v-if="selectable"></th>
<template v-for="(column, index) in columns" :key="index">
<th
v-if="!column.hidden"
:class="getClasses(column)"
>
<slot
name="column-filter"
:column="column"
:updateFilters="updateSlotFilter"
>
<div
v-if="isFilterable(column)">
<input v-if="!isDropdown(column)"
:name="getName(column)"
type="text"
class="vgt-input"
:placeholder="getPlaceholder(column)"
:value="columnFilters[fieldKey(column.field)]"
@keyup.enter="updateFiltersOnEnter(column, $event.target.value)"
@input="updateFiltersOnKeyup(column, $event.target.value)"/>
<!-- options are a list of primitives -->
<select v-if="isDropdownArray(column)"
:name="getName(column)"
class="vgt-select"
:value="columnFilters[fieldKey(column.field)]"
@change="updateFiltersImmediately(column.field, $event.target.value)">
<option value="" key="-1">{{ getPlaceholder(column) }}</option>
<option
v-for="(option, i) in column.filterOptions.filterDropdownItems"
:key="i"
:value="option">
{{ option }}
</option>
</select>
<!-- options are a list of objects with text and value -->
<select v-if="isDropdownObjects(column)"
:name="getName(column)"
class="vgt-select"
:value="columnFilters[fieldKey(column.field)]"
@change="updateFiltersImmediately(column.field, $event.target.value)">
<option value="" key="-1">{{ getPlaceholder(column) }}</option>
<option
v-for="(option, i) in column.filterOptions.filterDropdownItems"
:key="i"
:value="option.value">{{ option.text }}
</option>
</select>
</div>
</slot>
</th>
</template>
</tr>
</template>
<script>
export default {
name: 'VgtFilterRow',
emits: ['filter-changed'],
props: [
'lineNumbers',
'columns',
'typedColumns',
'globalSearchEnabled',
'selectable',
'mode',
],
watch: {
columns: {
handler (newValue, oldValue) {
this.populateInitialFilters()
},
deep: true,
immediate: true,
},
},
data () {
return {
columnFilters: {},
timer: null,
}
},
computed: {
// to create a filter row, we need to
// make sure that there is atleast 1 column
// that requires filtering
hasFilterRow () {
// if (this.mode === 'remote' || !this.globalSearchEnabled) {
for (let i = 0; i < this.columns.length; i++) {
const col = this.columns[i]
if (col.filterOptions && col.filterOptions.enabled) {
return true
}
}
// }
return false
},
},
methods: {
fieldKey (field) {
if (typeof (field) === 'function' && field.name) {
return field.name
}
return field
},
reset (emitEvent = false) {
this.columnFilters = {}
if (emitEvent) {
this.$emit('filter-changed', this.columnFilters)
}
},
isFilterable (column) {
return column.filterOptions
&& column.filterOptions.enabled
},
isDropdown (column) {
return this.isFilterable(column)
&& column.filterOptions.filterDropdownItems
&& column.filterOptions.filterDropdownItems.length
},
isDropdownObjects (column) {
return this.isDropdown(column)
&& typeof column.filterOptions.filterDropdownItems[0] === 'object'
},
isDropdownArray (column) {
return this.isDropdown(column)
&& typeof column.filterOptions.filterDropdownItems[0] !== 'object'
},
getClasses (column) {
const firstClass = 'filter-th'
return (column.filterOptions && column.filterOptions.styleClass) ? [firstClass, ...column.filterOptions.styleClass.split(' ')].join(' ') : firstClass
},
// get column's defined placeholder or default one
getPlaceholder (column) {
const placeholder = (this.isFilterable(column) && column.filterOptions.placeholder) || `Filter ${column.label}`
return placeholder
},
getName (column) {
return `vgt-${this.fieldKey(column.field)}`
},
updateFiltersOnEnter (column, value) {
if (this.timer) clearTimeout(this.timer)
this.updateFiltersImmediately(column.field, value)
},
updateFiltersOnKeyup (column, value) {
// if the trigger is enter, we don't filter on keyup
if (column.filterOptions.trigger === 'enter') return
this.updateFilters(column, value)
},
updateSlotFilter (column, value) {
let fieldToFilter = column.filterOptions.slotFilterField || column.field
if (typeof column.filterOptions.formatValue === 'function') {
value = column.filterOptions.formatValue(value)
}
this.updateFiltersImmediately(fieldToFilter, value)
},
// since vue doesn't detect property addition and deletion, we
// need to create helper function to set property etc
updateFilters (column, value) {
if (this.timer) clearTimeout(this.timer)
this.timer = setTimeout(() => {
this.updateFiltersImmediately(column.field, value)
}, 400)
},
updateFiltersImmediately (field, value) {
let key = this.fieldKey(field)
this.columnFilters[key] = value
this.$emit('filter-changed', this.columnFilters)
},
populateInitialFilters () {
for (let i = 0; i < this.columns.length; i++) {
const col = this.columns[i]
// lets see if there are initial
// filters supplied by user
if (this.isFilterable(col)
&& typeof col.filterOptions.filterValue !== 'undefined'
&& col.filterOptions.filterValue !== null) {
let key = this.fieldKey(col.field)
this.columnFilters[key] = col.filterOptions.filterValue
// this.updateFilters(col, col.filterOptions.filterValue);
}
}
//* lets emit event once all filters are set
this.$emit('filter-changed', this.columnFilters)
},
},
}
</script>
<style scoped>
</style>