I'm currently working on sorting functionality on a table built with vue.js. I have ascending sorting currently working with numbers. However I am unable to get the descending and alphabetical functionality to work properly.
Here is my html where I am calling the sort functionality. Currently I have this as a method().
<tr class="dash-table-mainHead">
<th
v-for="(column, key) in columns"
:key="key"
@click="sortTable(column)"
>{{ column.label }}</th>
</tr>
Here is the javascript where I have my array of columns with appropriate fields.
data() {
return {
columns: [
{ label: this.$t('reporting.source'), field: 'source' },
{ label: this.$t('reporting.accountsWithActivity'), field: 'accountsWithActivity', align: 'center', type: 'icon' },
{ label: this.$t('reporting.answerableConversations'), field: 'answerableConversations', type: 'boolean', align: 'center' },
{ label: this.$t('reporting.interactiveConversations'), field: 'interactiveConversations', type: 'boolean', align: 'center' },
{ label: this.$t('reporting.leads'), field: 'leads', align: 'center' },
{ label: this.$t('reporting.interactiveLeadConversations'), field: 'leads', type: 'date' },
{ field: 'blank' },
],
convertedData: [],
// currentSort: 'name',
currentSortDir: 'asc',
}
},
And finally my method where I am doing the sorting. This currently works for ascending only and doesn't appear to be sorting alphabetically.
methods: { sortTable(column) {
console.log(column.field)
let sortedData = [];
sortedData = this.convertedData.sort((a, b) => {
if (a[column.field] < b[column.field]) { return -1; }
if (a[column.field] > b[column.field]) { return 1; }
return 0;
})
}
}