Skip to content

Commit 9134227

Browse files
gmentimarcosmoura
authored andcommitted
fix(MdTable): fix error to sort by attribute of child object (#1309)
* Fix error to sort by attribute of child object * Fix attribute name * Remove blank lines * refactor(MdTable): cache attributes
1 parent 49787d1 commit 9134227

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/components/MdTable/MdTable.vue

+14-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060
import MdTableRowGhost from './MdTableRowGhost'
6161
import MdTableCellSelection from './MdTableCellSelection'
6262
63+
const getObjectAttribute = (object, key) => {
64+
let value = object
65+
66+
for (const attribute of key.split('.')) {
67+
value = value[attribute]
68+
}
69+
70+
return value
71+
}
72+
6373
export default {
6474
name: 'MdTable',
6575
components: {
@@ -93,12 +103,14 @@
93103
default (value) {
94104
return value.sort((a, b) => {
95105
const sortBy = this.MdTable.sort
106+
const aAttr = getObjectAttribute(a, sortBy)
107+
const bAttr = getObjectAttribute(b, sortBy)
96108
97109
if (this.MdTable.sortOrder === 'desc') {
98-
return a[sortBy].localeCompare(b[sortBy])
110+
return aAttr.localeCompare(bAttr)
99111
}
100112
101-
return b[sortBy].localeCompare(a[sortBy])
113+
return bAttr.localeCompare(aAttr)
102114
})
103115
}
104116
}

0 commit comments

Comments
 (0)