Skip to content

Commit 32363a4

Browse files
committed
fix: CDataTable - fix sorting function and items watcher
1 parent 657d0e9 commit 32363a4

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

src/components/table/CDataTable.vue

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,12 @@ export default {
298298
},
299299
items (val, oldVal) {
300300
if (
301-
val.length !== oldVal.length ||
302-
JSON.stringify(val) !== JSON.stringify(oldVal)
301+
val && oldVal && val.length === oldVal.length &&
302+
JSON.stringify(val) === JSON.stringify(oldVal)
303303
) {
304-
this.passedItems = val
304+
return
305305
}
306+
this.passedItems = val || []
306307
},
307308
totalPages: {
308309
immediate: true,
@@ -313,7 +314,7 @@ export default {
313314
},
314315
computed: {
315316
columnFiltered () {
316-
let items = this.passedItems.slice()
317+
let items = this.passedItems
317318
if (this.columnFilter && this.columnFilter.external) {
318319
return items
319320
}
@@ -333,7 +334,7 @@ export default {
333334
})
334335
},
335336
tableFiltered () {
336-
let items = this.columnFiltered.slice()
337+
let items = this.columnFiltered
337338
if (!this.tableFilterState || (this.tableFilter && this.tableFilter.external)) {
338339
return items
339340
}
@@ -349,10 +350,15 @@ export default {
349350
if (!col || !this.rawColumnNames.includes(col) || this.sorter.external) {
350351
return this.tableFiltered
351352
}
353+
352354
//if values in column are to be sorted by numeric value they all have to be type number
353355
const flip = this.sorterState.asc ? 1 : -1
354-
return this.tableFiltered.slice().sort((a,b) => {
355-
return (a[col] > b[col]) ? 1 * flip : ((b[col] > a[col]) ? -1 * flip : 0)
356+
return this.tableFiltered.slice().sort((item , item2) => {
357+
const value = item[col]
358+
const value2 = item2[col]
359+
const a = typeof value === 'number' ? value : String(value)
360+
const b = typeof value2 === 'number' ? value2 : String(value2)
361+
return a > b ? 1 * flip : b > a ? -1 * flip : 0
356362
})
357363
},
358364
firstItemIndex () {

src/components/table/tests/CDataTable.spec.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const ComponentName = 'CDataTable'
55
const defaultWrapper = mount(Component)
66

77
const items = [
8-
{username: 'Estavan Lykos', registered: '2012/02/01', role: 'Staff', status: 'Banned'},
9-
{username: 'Chetan Mohamed', registered: '2012/02/01', role: 'Admin', status: 'Inactive'},
10-
{username: 'Derick Maximinus', registered: '2012/03/01', role: 'Member', status: 'Pending'},
11-
{username: 'Yiorgos Avraamu', registered: '2012/01/01', role: 'Member', status: 'Active'},
8+
{username: 'Estavan Lykos', registered: 2014, role: 'Staff', status: 'Banned'},
9+
{username: 'Chetan Mohamed', registered: 2011, role: 'Admin', status: 'Inactive'},
10+
{username: 'Derick Maximinus', registered: 212, role: 'Member', status: 'Pending'},
11+
{username: 'Yiorgos Avraamu', registered: 2013, role: 'Member', status: 'Active'},
1212
{
1313
username: 'Friderik Dávid',
14-
registered: '2011/01/21',
14+
registered: 1999,
1515
role: 'Staff',
1616
status: 'Active',
1717
_cellClasses: { registered: 'custom-cell-class' }
@@ -45,7 +45,7 @@ function createCustomWrapper () {
4545
columnFilter: true,
4646
footer: true,
4747
sorterValue: { column: 'username', asc: false },
48-
columnFilterValue: { registered: '2012', 'non_existing': 'smh' },
48+
columnFilterValue: { registered: '2', 'non_existing': 'smh' },
4949
pagination: true
5050
}
5151
})
@@ -68,6 +68,9 @@ describe(ComponentName, () => {
6868

6969
customWrapper.find('tr').findAll('th').at(3).trigger('click')
7070
expect(customWrapper.vm.sortedItems[0].status).toBe('Pending')
71+
72+
customWrapper.find('tr').findAll('th').at(1).trigger('click')
73+
expect(customWrapper.vm.sortedItems[0].registered).toBe(212)
7174
})
7275
it('doesnt change sorter when clicked on not sortable column', () => {
7376
const oldSorterColumn = customWrapper.vm.sorter.column
@@ -91,15 +94,12 @@ describe(ComponentName, () => {
9194
customWrapper.find('tbody').find('tr').trigger('click')
9295
expect(customWrapper.emitted()['row-clicked']).toBeTruthy()
9396
})
94-
it('correctly updates items', () => {
95-
//test if watcher is not fired by coverage
97+
it('correctly triggers items update', () => {
9698
const localWrapper = createCustomWrapper()
99+
//set to cover branch, could not detect if computed prop is changed
97100
localWrapper.setProps({ items: items.slice() })
98-
expect(localWrapper.vm.sortedItems.length).toBe(4)
99-
100-
const newItems = items.slice(0, 2)
101-
localWrapper.setProps({ items: newItems })
102-
expect(localWrapper.vm.sortedItems.length).toBe(2)
101+
localWrapper.setProps({ items: null })
102+
expect(localWrapper.vm.columnFiltered.length).toBe(0)
103103
})
104104
it('updates column filter on events depending on lazy modifier', () => {
105105
const localWrapper = createCustomWrapper()

src/components/table/tests/__snapshots__/CDataTable.spec.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ exports[`CDataTable renders correctly 2`] = `
303303
class=""
304304
>
305305
306-
2012/01/01
306+
2013
307307
308308
</td>
309309
<td
@@ -342,7 +342,7 @@ exports[`CDataTable renders correctly 2`] = `
342342
class=""
343343
>
344344
345-
2012/02/01
345+
2014
346346
347347
</td>
348348
<td
@@ -381,7 +381,7 @@ exports[`CDataTable renders correctly 2`] = `
381381
class=""
382382
>
383383
384-
2012/03/01
384+
212
385385
386386
</td>
387387
<td
@@ -420,7 +420,7 @@ exports[`CDataTable renders correctly 2`] = `
420420
class=""
421421
>
422422
423-
2012/02/01
423+
2011
424424
425425
</td>
426426
<td

0 commit comments

Comments
 (0)