gpt4 book ai didi

javascript - 如何在 VueJS 中添加自定义排序?

转载 作者:行者123 更新时间:2023-11-30 19:05:26 25 4
gpt4 key购买 nike

我这里有一个数据结构,我将其分为标题和项目。我正在尝试应用一个自定义排序,我已经为它创建了一个方法,但我无法弄清楚如何根据名称和等级分别应用该排序方法。这是 codepen 的链接.

new Vue({
el: '#app',
data() {
return {
headers: [{
text: 'Name',
value: 'Name'
},
{
text: 'Grades',
value: 'grades'
},
],
labels: ['Andy', 'Max', 'John', 'Travis', 'Rick'],
Grades: [99, 72, 66, 84, 91]
}
},
computed: {
tableItems() {
let items = [],
this.labels.forEach((label, i) => {
let item = {}
item.name = label
item.grades = this.Grades[i]
items.push(item)
console.log(items)
})
return items
}
},
methods: {
sortBy(prop) {
this.tableItems.sort((a, b) => a[prop] < b[prop] ? -1 : 1)
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vuetify@1.5.14/dist/vuetify.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/vuetify@1.5.14/dist/vuetify.min.css" rel="stylesheet" />
<div id="app">
<v-app id="inspire">
<v-container>
<v-layout>
<v-flex v-for="(header, i) in headers" :key="header.text" xs4 py-1>
<span>{{ header.text }}
<v-icon small @click="sortBy(this.labels)">arrow_upward</v-icon>
</span>
</v-flex>
<v-layout v-for="item in tableItems" :key="item.name">
<v-flex xs4 py-1>
<span>{{ item.name }}</span>
</v-flex>
<v-flex xs4 py-1>
<span>{{item.grades}}</span>
</v-flex>

</v-layout>
</v-layout>
</v-container>
</v-app>
</div>

现在,在保持数据结构和所有内容不变的情况下,我如何才能使 v-icon 上的方法分别适用于成绩和姓名?

最佳答案

以下是我为让它发挥作用所做的一些事情。

  • 定义一个 sortKey,您的数据将以此为基础进行排序
  • 当您点击 span 时将 sortKey 作为参数传递
  • 在计算您的 tableItems 时,使用排序键对您的数据进行排序

在下面的代码中寻找注释

<div id="app">
<v-app id="inspire">
<v-container>
<v-layout>
<v-flex v-for="header in headers" :key="header.text" xs4 py-1>
<span>
{{ header.text }}
<v-icon small @click="sortBy(header.value)">arrow_upward</v-icon>
***** changed the value for name (from Name to name) ****
</span>
*** everything else is the same ***
</v-layout>
</v-container>
</v-app>
</div>

脚本是这样的

    <script>
export default {
name: "app",
data() {
return {
headers: [
{ text: "Name", value: "name" }, // changed this to name
{ text: "Grades", value: "grades" }
],
labels: ["Andy", "Max", "John", "Travis", "Rick"],
Grades: [99, 72, 66, 84, 91],
sortKey: "" // added a sortKey,
};
},
computed: {
tableItems() {
let retVal = this.labels.map((label, i) => {
return {
name: label,
grades: this.Grades[i]
};
});
// if there is a sortKey use that
if (this.sortKey) {
retVal.sort((a, b) =>
a[this.sortKey] < b[this.sortKey] ? -1 : 1
);
}
return retVal;
}
},
methods: {
sortBy(prop) {
// assign sortKey here, this assignment will retrigger the computation
this.sortKey = prop;
console.log(prop);
}
}
};
</script>

*************** 编辑 *****************

为您的排序顺序添加一个方向变量(1 为升序,-1 为降序)

所以你的数据看起来像

data() {
return {
headers: [
{ text: "Name", value: "name" }, // changed this to name
{ text: "Grades", value: "grades" }
],
labels: ["Andy", "Max", "John", "Travis", "Rick"],
Grades: [99, 72, 66, 84, 91],
sortKey: "" // added a sortKey,
direction: 1 // for ascending order
};
},

现在在你的程序中,如果你点击相同的东西,你需要改变你的升序或降序状态,在你的方法中做这件事

methods: {
sortBy(prop) {
// if the sortKey was prop to begin with
if (this.sortKey === prop) {
this.direction *= -1 // change direction to -ve or positive
}
// assign sortKey here, this assignment will retrigger the computation
this.sortKey = prop;
console.log(prop);
}
}

最后,在排序中使用你的方向

computed: {
tableItems() {
let retVal = this.labels.map((label, i) => {
return {
name: label,
grades: this.Grades[i]
};
});
// if there is a sortKey use that
if (this.sortKey) {
retVal.sort((a, b) =>
this.direction * // here multiply by the direction
(a[this.sortKey] < b[this.sortKey] ? -1 : 1)
);
}
return retVal;
}
},

然后你就完成了

关于javascript - 如何在 VueJS 中添加自定义排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59038693/

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