2 回答
TA贡献1803条经验 获得超3个赞
这是如何执行此操作的示例。因此,我们有一个辅助方法getSorter,它查看您的v-model指令绑定到的当前选定值this.value,并返回一个适当的函数以传递给基于该值的排序。如果没有选择任何内容,它将返回null。
在您的计算属性中filteredSearch,您可以根据需要应用现有的过滤器,然后对其结果进行排序。
methods: {
// determine the sorting function to use
getSorter() {
switch (this.value) {
case 'Za':
return (a,b) => b.topic > a.topic ? 1 : a.topic == b.topic ? 0 : -1;
case 'Az':
return (a,b) => b.topic > a.topic ? -1 : a.topic == b.topic ? 0 : 1;
case 'Ph':
return (a,b) => b.price - a.price;
case 'Pl':
return (a,b) => a.price - b.price;
default:
return null;
}
}
},
computed: {
filteredSearch: function() {
// apply existing filter logic
var filteredProducts = this.products
.filter((el) => true); // replace with your existing filter conditions here
// apply sort function
var sortFunction = this.getSorter();
return sortFunction ? filteredProducts.sort(sortFunction) : filteredProducts;
}
}
TA贡献1799条经验 获得超8个赞
我认为你不应该在计算属性函数中使用过滤器,最好只做这样的排序
filteredSearch() {
return !this.value ?
true
: this.value == "Az" ?
this.products.sort((a, b) => a.topic > b.topic ? 1 : -1)
: this.value == "Za" ?
this.products.sort((a, b) => a.topic < b.topic ? 1 : -1)
: this.value == "Pl" ?
this.products.sort((a, b) => a.price - b.price)
: this.value == "Ph" ?
this.products.sort((a, b) => b.price - a.price)
: null;
}
添加回答
举报
