Я работаю над небольшим проектом. Что я хочу сделать, так это когда происходит сортировка @click событие, столбец sortcol задается asc, desc, но. То, что я хочу, это то, что я хочу очистить значение других столбцов, кроме столбца, по которому щелкнули.
Я не мог понять, как это сделать.
<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>



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вы можете очистить свой объект sortcol, если свойство есть:
const app = Vue.createApp({
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: {},
items: [{id: 1, name: 'aa', position: 2, office: 'AA', extension: 'tt', startdate: '12', salary: 55}, {id: 3, name: 'cc', position: 1, office: 'CC', extension: 'xx', startdate: '82', salary: 75}, {id: 2, name: 'bb', position: 8, office: 'BB', extension: 'zz', startdate: '15', salary: 35}]
}
},
methods: {
sortItems(column, col) {
this.items = this.items.sort((a, b) => {
return this.sortcol[column] === 'asc' ? a[col] > b[col] : a[col] < b[col]
})
},
sortByColumn(column) {
const selected = column === Object.keys(this.sortcol)[0]
if (selected) {
this.sortcol[column] = this.sortcol[column] === 'asc' ? 'desc' : 'asc'
} else {
this.sortcol = {}
this.sortcol[column] = 'asc'
}
this.sortItems(column, Object.keys(this.sortcol))
},
},
computed: {
tableHeader() {
return this.columns
},
}
})
app.mount('#demo')<script src = "https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id = "demo">
<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>
<tbody>
<tr v-for = "item in items">
<td v-for = "itm in item">
{{ itm }}
</td>
</tr>
</tbody>
</table>
</div>$array.sorted Если и еще в нужном месте, я не могу понять это прямо сейчас
Сначала создайте массив доступных ключей из sortcol. Затем удалите th.name, переданный функции. Затем вы можете удалить значения sortcol после выполнения цикла for..of.
new Vue({
el: "#app",
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]
// Solution
const colArr = Object.keys(this.sortcol)
const extra = colArr.filter(e => e !== column)
for (i of extra) {
this.sortcol[i] = ''
}
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 src = "https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id = "app">
<table>
<thead>
<tr>
<th v-for = "th in columns" :key = "th.name">
<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>
</div>
Спасибо за твой ответ. это будет так?
let sortedEntries = this.getCurrentEntries() const sortedColumn = column === Object.keys(this.sortcol)[0] if (sortedColumn) { this.sortcol[column] = this.sortcol[column] === 'asc' ? 'desc' : 'asc' sortedEntries = $array.sorted(this.getCurrentEntries(), column, 'desc') } else { sortedEntries = $array.sorted(this.getCurrentEntries(), column, 'asc') this.sortcol = {} this.sortcol[column] = 'asc' }