10
10
class =" form-control table-filter"
11
11
type =" text"
12
12
placeholder =" type string..."
13
- @input =" tableFilterVal = $event.target.value"
14
- :value =" tableFilterVal"
13
+ @input =" tableFilterChange($event.target.value)"
14
+ @change =" tableFilterChange($event.target.value, 'change')"
15
+ :value =" tableFilterState"
15
16
>
16
17
</div >
17
18
77
78
<th :class =" headerClass(index)" :key =" index" >
78
79
<slot :name =" `${rawColumnNames[index]}-filter`" >
79
80
<input
80
- v-if =" !fields || ! fields[index].filterable !== false"
81
+ v-if =" !fields || fields[index].filter !== false"
81
82
class =" w-100 table-filter"
82
- @input =" addColumnFilter(colName, $event.target.value)"
83
- :value =" columnFilterVal[colName]"
83
+ @input =" columnFilterEvent(colName, $event.target.value, 'input')"
84
+ @change =" columnFilterEvent(colName, $event.target.value, 'change')"
85
+ :value =" columnFilterState[colName]"
84
86
/>
85
87
</slot >
86
88
</th >
@@ -227,14 +229,12 @@ export default {
227
229
default: 10
228
230
},
229
231
activePage: Number ,
230
- columnFilter: Boolean ,
231
232
pagination: [Boolean , Object ],
232
233
addTableClasses: [String , Array , Object ],
233
234
responsive: {
234
235
type: Boolean ,
235
236
default: true
236
237
},
237
- sortable: Boolean ,
238
238
size: String ,
239
239
dark: Boolean ,
240
240
striped: Boolean ,
@@ -243,41 +243,85 @@ export default {
243
243
border: Boolean ,
244
244
outlined: Boolean ,
245
245
itemsPerPageSelect: Boolean ,
246
- tableFilter: Boolean ,
247
- footer: Boolean ,
248
- defaultSorter: {
246
+ sorter: [Boolean , String ],
247
+ tableFilter: [Boolean , String ],
248
+ columnFilter: [Boolean , String ],
249
+ sorterValue: {
249
250
type: Object ,
250
251
default : () => { return {} }
251
252
},
252
- defaultTableFilter: String ,
253
- defaultColumnFilter: Object ,
253
+ tableFilterValue: String ,
254
+ columnFilterValue: Object ,
255
+ footer: Boolean ,
254
256
loading: Boolean ,
255
257
clickableRows: Boolean
256
258
},
257
259
data () {
258
260
return {
259
- tableFilterVal : this .defaultTableFilter ,
260
- columnFilterVal : this . defaultColumnFilter || {},
261
- sorter : {
262
- column: this . defaultSorter . column || null ,
263
- asc: this . defaultSorter . asc === false ? false : true
261
+ tableFilterState : this .tableFilterValue ,
262
+ columnFilterState : {},
263
+ sorterState : {
264
+ column: undefined ,
265
+ asc: true
264
266
},
265
267
page: this .activePage || 1 ,
266
268
perPageItems: this .itemsPerPage ,
267
269
passedItems: this .items || []
268
270
}
269
271
},
272
+ watch: {
273
+ sorterValue: {
274
+ immediate: true ,
275
+ handler (val ) {
276
+ this .sorterState .column = val .column
277
+ this .sorterState .asc = val .asc === false ? false : true
278
+ }
279
+ },
280
+ tableFilterValue (val ) {
281
+ this .tableFilterState = val
282
+ },
283
+ columnFilterValue: {
284
+ immediate: true ,
285
+ handler (val ) {
286
+ this .columnFilterState = Object .assign ({}, val)
287
+ }
288
+ // const state = this.columnFilterState
289
+ // const currentColumns = Object.keys(state)
290
+ // Object.keys(val).forEach(colName => {
291
+ // if (!currentColumns.includes(colName)) {
292
+ // this.setColumnFilter(colName, val[colName] || '')
293
+ // }
294
+ // })
295
+ // currentColumns.forEach(colName => state[colName] = val[colName] || '')
296
+ },
297
+ items (val , oldVal ) {
298
+ if (
299
+ val .length !== oldVal .length ||
300
+ JSON .stringify (val) !== JSON .stringify (oldVal)
301
+ ) {
302
+ this .passedItems = val
303
+ }
304
+ },
305
+ totalPages: {
306
+ immediate: true ,
307
+ handler (val ) {
308
+ this .$emit (' pages-change' , val)
309
+ }
310
+ }
311
+ },
270
312
computed: {
271
313
columnFiltered () {
272
314
let items = this .passedItems .slice ()
273
- Object .entries (this .columnFilterVal ).forEach (([key , value ]) => {
274
- if (value && this .rawColumnNames .includes (key)) {
275
- const columnFilter = String (value).toLowerCase ()
276
- items = items .filter (item => {
277
- return String (item[key]).toLowerCase ().includes (columnFilter)
278
- })
279
- }
280
- })
315
+ if (this .columnFilter === true ) {
316
+ Object .entries (this .columnFilterState ).forEach (([key , value ]) => {
317
+ if (value && this .rawColumnNames .includes (key)) {
318
+ const columnFilter = String (value).toLowerCase ()
319
+ items = items .filter (item => {
320
+ return String (item[key]).toLowerCase ().includes (columnFilter)
321
+ })
322
+ }
323
+ })
324
+ }
281
325
return items
282
326
},
283
327
filterableCols () {
@@ -287,8 +331,8 @@ export default {
287
331
},
288
332
tableFiltered () {
289
333
let items = this .columnFiltered .slice ()
290
- if (this .tableFilterVal ) {
291
- const filter = this .tableFilterVal .toLowerCase ()
334
+ if (this .tableFilter === true && this . tableFilterState ) {
335
+ const filter = this .tableFilterState .toLowerCase ()
292
336
const hasFilter = (item ) => String (item).toLowerCase ().includes (filter)
293
337
items = items .filter (item => {
294
338
return this .filterableCols .filter (key => hasFilter (item[key])).length
@@ -297,12 +341,12 @@ export default {
297
341
return items
298
342
},
299
343
sortedItems () {
300
- const col = this .sorter .column
301
- if (! col || ! this .rawColumnNames .includes (col)) {
344
+ const col = this .sorterState .column
345
+ if (! col || this . sorter !== true || ! this .rawColumnNames .includes (col)) {
302
346
return this .tableFiltered
303
347
}
304
348
// if values in column are to be sorted by numeric value they all have to be type number
305
- const flip = this .sorter .asc ? 1 : - 1
349
+ const flip = this .sorterState .asc ? 1 : - 1
306
350
return this .tableFiltered .slice ().sort ((a ,b ) => {
307
351
return (a[col] > b[col]) ? 1 * flip : ((b[col] > a[col]) ? - 1 * flip : 0 )
308
352
})
@@ -358,48 +402,44 @@ export default {
358
402
]
359
403
},
360
404
sortingIconStyles () {
361
- return {' position-relative pr-4' : this .sortable }
405
+ return {' position-relative pr-4' : this .sorter }
362
406
},
363
407
colspan () {
364
408
return this .rawColumnNames .length
365
409
},
366
- isFiltered () {
367
- return this .tableFilterVal || Object .values (this .columnFilterVal ).join (' ' )
368
- }
369
- },
370
- watch: {
371
- items (val , oldVal ) {
372
- if (
373
- val .length !== oldVal .length ||
374
- JSON .stringify (val) !== JSON .stringify (oldVal)
375
- ) {
376
- this .passedItems = val
377
- }
378
- },
379
- totalPages: {
380
- immediate: true ,
381
- handler (val ) {
382
- this .$emit (' pages-change' , val)
383
- }
384
- }
410
+ // isFiltered () {
411
+ // return this.tableFilterState || Object.values(this.columnFilterState).join('')
412
+ // }
385
413
},
386
414
methods: {
387
415
changeSort (column , index ) {
388
416
if (column && ! this .isSortable (index)) {
389
417
return
390
418
}
391
419
// if column changed or sort was descending change asc to true
392
- this .sorter .asc = this .sorter .column !== column || ! this .sorter .asc
393
- this .sorter .column = column
420
+ const state = this .sorterState
421
+ state .asc = state .column !== column || ! state .asc
422
+ state .column = column
423
+ this .$emit (' update:sorter-value' , this .sorterState )
424
+ },
425
+ columnFilterEvent (colName , value , type ) {
426
+ this .setColumnFilter (colName, value)
427
+ const e = type === ' input' ? ' column-filter-input' : ' update:column-filter-value'
428
+ this .$emit (e, this .columnFilterState )
429
+ },
430
+ setColumnFilter (colName , value ) {
431
+ this .$set (this .columnFilterState , colName, value)
394
432
},
395
- addColumnFilter (colName , value ) {
396
- this .$set (this .columnFilterVal , colName, value)
433
+ tableFilterChange (value , type = ' input' ) {
434
+ this .tableFilterState = value
435
+ const e = type === ' input' ? ' table-filter-input' : ' update:table-filter-value'
436
+ this .$emit (e, this .tableFilterState )
397
437
},
398
438
// clear () {
399
- // this.tableFilterVal = ''
400
- // this.columnFilterVal = {}
401
- // this.sorter .column = ''
402
- // this.sorter .asc = true
439
+ // this.tableFilterState = ''
440
+ // this.columnFilterState = {}
441
+ // this.sorterState .column = ''
442
+ // this.sorterState .asc = true
403
443
// const inputs = this.$el.getElementsByClassName('table-filter')
404
444
// for (let input of inputs) {
405
445
// input.value = ''
@@ -424,7 +464,7 @@ export default {
424
464
return classes
425
465
},
426
466
isSortable (index ) {
427
- return this .sortable && (! this .fields || this .fields [index].sortable !== false )
467
+ return this .sorter && (! this .fields || this .fields [index].sorter !== false )
428
468
},
429
469
headerClass (index ) {
430
470
const fields = this .fields
@@ -444,8 +484,8 @@ export default {
444
484
this .$emit (' row-clicked' , item, index)
445
485
},
446
486
getIconState (index ) {
447
- const direction = this .sorter .asc ? ' asc' : ' desc'
448
- return this .rawColumnNames [index] === this .sorter .column ? direction : 0
487
+ const direction = this .sorterState .asc ? ' asc' : ' desc'
488
+ return this .rawColumnNames [index] === this .sorterState .column ? direction : 0
449
489
},
450
490
iconClasses (index ) {
451
491
const state = this .getIconState (index)
0 commit comments