@@ -254,8 +254,8 @@ export default {
254
254
optionsRow: [Boolean , String ],
255
255
footer: Boolean ,
256
256
defaultSorter: {
257
- type: Array ,
258
- default : () => []
257
+ type: Object ,
258
+ default : () => { return {} }
259
259
},
260
260
defaultTableFilter: String ,
261
261
defaultColumnFilter: Object ,
@@ -265,7 +265,10 @@ export default {
265
265
return {
266
266
tableFilter: this .defaultTableFilter ,
267
267
columnFilter: this .defaultColumnFilter || {},
268
- sorter: { name: this .defaultSorter [0 ], direction: this .defaultSorter [1 ] },
268
+ sorter: {
269
+ column: this .defaultSorter .column || null ,
270
+ asc: this .defaultSorter .asc || true
271
+ },
269
272
firstItemIndex: 0 ,
270
273
page: this .activePage || 1 ,
271
274
perPageItems: this .perPage ,
@@ -290,17 +293,20 @@ export default {
290
293
return items
291
294
},
292
295
sortedItems () {
296
+ const col = this .sorter .column
297
+ if (! col || ! this .rawColumnNames .includes (col)) {
298
+ return this .tableFiltered
299
+ }
293
300
// if numbers should be sorted by numeric value they all have to be valid js numbers
294
- const flip = this .sorter .direction === ' desc' ? - 1 : 1
295
- const n = this .sorter .name
301
+ const flip = this .sorter .asc ? 1 : - 1
296
302
return this .tableFiltered .sort ((a ,b ) => {
297
303
// escape html
298
- let c = typeof a[n ] === ' string' ? a[n ].replace (/ <(?:. | \n )*? >/ gm , ' ' ) : a[n ]
299
- let d = typeof b[n ] === ' string' ? b[n ].replace (/ <(?:. | \n )*? >/ gm , ' ' ) : b[n ]
300
- if (typeof c !== typeof d) {
301
- c = String (c)
302
- d = String (d)
303
- }
304
+ let c = typeof a[col ] === ' string' ? a[col ].replace (/ <(?:. | \n )*? >/ gm , ' ' ) : a[col ]
305
+ let d = typeof b[col ] === ' string' ? b[col ].replace (/ <(?:. | \n )*? >/ gm , ' ' ) : b[col ]
306
+ // if (typeof c !== typeof d) {
307
+ // c = String(c)
308
+ // d = String(d)
309
+ // }
304
310
return (c > d) ? 1 * flip : ((d > c) ? - 1 * flip : 0 )
305
311
})
306
312
},
@@ -381,15 +387,13 @@ export default {
381
387
}
382
388
},
383
389
methods: {
384
- changeSort (name , index ) {
385
- if (index && ! this .sortable (index))
390
+ changeSort (column , index ) {
391
+ if (index && ! this .sortable (index)) {
386
392
return
387
-
388
- if (this .sorter .name === name && ! this .sorter .direction )
389
- this .sorter .direction = ' desc'
390
- else
391
- this .sorter .direction = 0
392
- this .sorter .name = name
393
+ }
394
+ // if column changed or sort was descending change asc to true
395
+ this .sorter .asc = this .sorter .column !== column || ! this .sorter .asc
396
+ this .sorter .column = column
393
397
},
394
398
addColumnFilter (colName , value ) {
395
399
this .$set (this .columnFilter , colName, value)
@@ -398,7 +402,7 @@ export default {
398
402
this .tableFilter = ' '
399
403
this .columnFilter = {}
400
404
this .sorter .name = ' '
401
- this .sorter .direction = ' '
405
+ this .sorter .asc = true
402
406
const inputs = this .$el .getElementsByClassName (' c-table-filter' )
403
407
for (let input of inputs)
404
408
input .value = ' '
@@ -436,9 +440,8 @@ export default {
436
440
this .$emit (' row-clicked' , item, index)
437
441
},
438
442
getIconState (index ) {
439
- return this .rawColumnNames [index] === this .sorter .name ?
440
- this .sorter .direction === ' desc' ?
441
- ' desc' : ' asc' : 0
443
+ const direction = this .sorter .asc ? ' asc' : ' desc'
444
+ return this .rawColumnNames [index] === this .sorter .column ? direction : 0
442
445
},
443
446
iconClasses (index ) {
444
447
const state = this .getIconState (index)
0 commit comments