gpt4 book ai didi

javascript - vue 点击按列清空排序

转载 作者:行者123 更新时间:2023-12-05 02:25:37 25 4
gpt4 key购买 nike

我正在做一个小项目。我想要做的是当排序发生 @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>

最佳答案

如果属性存在,您可以清空 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>

关于javascript - vue 点击按列清空排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74419938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com