Skip to content

Commit c593ad9

Browse files
committed
refactor: change sorting mechanism in CTable component
1 parent ee86b1e commit c593ad9

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/components/Table/CTable.vue

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ export default {
254254
optionsRow: [Boolean, String],
255255
footer: Boolean,
256256
defaultSorter: {
257-
type: Array,
258-
default: () => []
257+
type: Object,
258+
default: () => { return {} }
259259
},
260260
defaultTableFilter: String,
261261
defaultColumnFilter: Object,
@@ -265,7 +265,10 @@ export default {
265265
return {
266266
tableFilter: this.defaultTableFilter,
267267
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+
},
269272
firstItemIndex: 0,
270273
page: this.activePage || 1,
271274
perPageItems: this.perPage,
@@ -290,17 +293,20 @@ export default {
290293
return items
291294
},
292295
sortedItems () {
296+
const col = this.sorter.column
297+
if (!col || !this.rawColumnNames.includes(col)) {
298+
return this.tableFiltered
299+
}
293300
//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
296302
return this.tableFiltered.sort((a,b) => {
297303
//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+
// }
304310
return (c > d) ? 1 * flip : ((d > c) ? -1 * flip : 0)
305311
})
306312
},
@@ -381,15 +387,13 @@ export default {
381387
}
382388
},
383389
methods: {
384-
changeSort (name, index) {
385-
if(index && !this.sortable(index))
390+
changeSort (column, index) {
391+
if (index && !this.sortable(index)) {
386392
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
393397
},
394398
addColumnFilter (colName, value) {
395399
this.$set(this.columnFilter, colName, value)
@@ -398,7 +402,7 @@ export default {
398402
this.tableFilter = ''
399403
this.columnFilter = {}
400404
this.sorter.name = ''
401-
this.sorter.direction = ''
405+
this.sorter.asc = true
402406
const inputs = this.$el.getElementsByClassName('c-table-filter')
403407
for(let input of inputs)
404408
input.value =''
@@ -436,9 +440,8 @@ export default {
436440
this.$emit('row-clicked', item, index)
437441
},
438442
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
442445
},
443446
iconClasses (index) {
444447
const state = this.getIconState(index)

0 commit comments

Comments
 (0)