我在Electron+VueJS2.x中有一个基本的应用程序(因为我是一个完全的初学者),我正在寻找一个对v-for列表的排序功能。我有一个半工作状态,我可以排序,但我想反向排序,如果第二次点击应用。我不确定解决这个问题的最佳方法,也许我现在要走的道路需要改进。请告诉我怎样才能做到这一点。
我用“selectedcategory”跟踪当前类别,如果它出现两次,就会运行
return this.items.reverse()
但我不能让它工作。
我已经清理了代码以帮助可读性。
export default {
name: 'home-page',
data() {
return {
selectedCategory: null,
items: [{
id: 1,
name: 'Person 1',
leave: 123.45
},
{
id: 2,
name: 'John Smith',
leave: 13.45
},
{
id: 3,
name: 'Bill Smith',
leave: 23.45
},
{
id: 4,
name: 'John Doe',
leave: 133.53
}
]
}
},
methods: {
sortedByName: function() {
function compare(a, b) {
if (a.name < b.name) {
return -1
}
if (a.name > b.name) {
return 1
}
return 0
}
this.selectedCategory = 'name'
return this.items.sort(compare)
},
sortedByNumber: function() {
// Same as above but a.leave and b.leave
},
sortedById: function() {
// Same as above but a.id and b.id
}
}
}
<template>
<a v-on:click="sortedById()">ID</a>
<a v-on:click="sortedByName()">User</a>
<a v-on:click="sortedByNumber()">Leave Owing</a>
<div id="page_list">
<div class="user_row" v-for="item in items">
<div class="user_status">{{ item.id }}</div>
<div class="username">{{ item.name }}</div>
<div class="leave_owing">{{ item.leave }}</div>
</div>
</div>
</template>
谢谢