I am currently working on adding the sort functionality to a table built with vue, I am not sure what I am missing, as my code gets the descending arrow but does not actually sort the column. I have added the table and the vue code for the table. Any help would be greatly appreciated.
HTML table
<v-simple-table>
<template v-slot:default>
<thead>
<tr>
<th class="text-center" @click="sort('name')">
<div>Batch ID</div>
<v-icon small v-if="sortBy === 'name'">{{descending ? 'fas fa-caret-down' : 'fas fa-caret-up'}}</v-icon>
</th>
<th class="text-center" @click="sort('strain')">
<div>Strain</div>
<v-icon small v-if="sortBy === 'strain'">{{descending ? 'fas fa-caret-down' : 'fas fa-caret-up'}}</v-icon>
</th>
<th class="text-center" @click="sort('type')">
<div>Type</div>
<v-icon small v-if="sortBy === 'type'">{{descending ? 'fas fa-caret-down' : 'fas fa-caret-up'}}</v-icon>
</th>
<th class="text-center" @click="sort('trim')">
<div>Trim</div>
<v-icon small v-if="sortBy === 'trim'">{{descending ? 'fas fa-caret-down' : 'fas fa-caret-up'}}</v-icon>
</th>
<th class="text-center" @click="sort('history')">
<div>History</div>
<v-icon small v-if="sortBy === 'history'">{{descending ? 'fas fa-caret-down' : 'fas fa-caret-up'}}</v-icon>
</th>
</tr>
</thead>
<tbody>
<tr v-for="item of filteredReports" :key="item.id">
<td class="text-center">{{item.name}}</td>
<td class="text-center">{{item.strain ? item.strain.name : '-'}}</td>
<td class="text-center">{{item.type.name}}</td>
<td class="text-center">
<phase-chip
:report="item.trim"
type="trim"
></phase-chip>
</td>
<td class="text-center">
<v-btn icon color="primary" small :to="`/batches/${item.batch}/history`" target="_blank">
<v-icon>fas fa-external-link-square-alt</v-icon>
</v-btn>
</td>
</tr>
</tbody>
</template>
</v-simple-table>
JS
methods : {
sort(field) {
if (this.sortBy === field) this.descending = !this.descending
else {
this.descending = true
this.sortBy = field
}
}
},
computed: {
sortedReports () {
return [...this.filteredReports].sort((a,b) => {
if (a[this.sortBy] < b[this.sortBy]) return this.descending ? 1 : -1
else if (a[this.sortBy] > b[this.sortBy]) return this.descending ? -1 : 1
else return 0
})
}
}
sortedReportsalready sorted in ascending and you just want to be able to toggle between descending and ascending?