I am working on a small project. What i want to do is when sorting happens @click event, sortcol column is given asc, desc, but. The thing I want is this, I want to clear the value of other columns except the clicked column.
I couldn't figure out how to get this done.
<template>
<table>
<thead>
<tr>
<th v-for="th in tableHeader" :key="th">
<div class="flex" @click.prevent="sortByColumn(th.name)">
<div class="sort-header-content">{{ th.text }}</div>
<div v-if="sortcol[th.name]==='asc'">ArrowDownIcon</div>
<div v-else-if="sortcol[th.name]==='desc'">ArrowUpIcon</div>
</div>
</th>
</tr>
</thead>
</table>
</template>
<script>
export default {
name: "Table",
data() {
return {
columns: [
{name: 'id', text: 'ID'},
{name: 'name', text: 'Name'},
{name: 'position', text: 'Position'},
{name: 'office', text: 'Office'},
{name: 'extension', text: 'Extension'},
{name: 'startdate', text: 'Start Date'},
{name: 'salary', text: 'Salary'},
],
sortcol: {
name: '',
position: '',
office: '',
extension: '',
startdate: '',
salary: '',
},
}
},
methods: {
sortByColumn(column) {
let sortedColumn = this.sortcol[column]
if (sortedColumn === '') {
this.sortcol[column] = 'asc'
} else if (sortedColumn === 'asc') {
this.sortcol[column] = 'desc'
} else if (sortedColumn === 'desc') {
this.sortcol[column] = ''
}
},
},
computed: {
tableHeader() {
return this.columns
},
}
}
</script>