// 商品排序 sortPrice() { if (this.value == 0) { this.goodsList = this.goodsList.sort((a, b) => { // console.log("000");0 return a.id - b.id; }); } else if (this.value == 1) { this.goodsList = this.goodsList.sort((a, b) => { // console.log(111); return b.price - a.price; }); } else if (this.value == 2) { this.goodsList = this.goodsList.sort((a, b) => { // console.log(222); return a.price - b.price; }); } else if (this.value == 3) { this.goodsList = this.goodsList.sort((a, b) => { // console.log(333); return b.volume - a.volume; }); } else if (this.value == 4) { this.goodsList = this.goodsList.sort((a, b) => { // console.log(444); return a.volume - b.volume; }); } }, // 点击有货优先时的回调函数 isHaveGoods() { // console.log(5555); // 只保留有货的商品 this.goodsList = this.goodsList.sort((a, b) => { return b.isHave - a.isHave; }); }, // 直营优先 isDirectGoods() { // console.log(6666); // 只保留有货的商品 this.goodsList = this.goodsList.sort((a, b) => { return b.isDirect - a.isDirect; }); },怎么把价格排序和直营优先排序结合起来
时间: 2024-03-03 17:46:59 浏览: 62
要把价格排序和直营优先排序结合起来,可以按照以下步骤进行:
1. 在前端页面上增加排序按钮或下拉菜单,包括价格排序和直营优先排序的选项。
2. 当用户选择价格排序选项时,调用 `sortPrice()` 函数进行价格排序。当用户选择直营优先排序选项时,调用 `isDirectGoods()` 函数进行直营优先排序。
3. 在 `sortPrice()` 函数中,增加直营优先的筛选条件,使排序结果中直营商品排在前面。具体实现可以在排序前先筛选出直营商品,然后再对直营商品和非直营商品分别进行价格排序,最后把排序后的直营商品和非直营商品合并起来。
4. 在 `isDirectGoods()` 函数中,增加价格排序的筛选条件,使排序结果中直营商品和非直营商品均按价格排序。具体实现可以先筛选出直营商品和非直营商品,然后分别对直营商品和非直营商品进行价格排序,最后把排序后的直营商品和非直营商品合并起来。
例如,在 `sortPrice()` 函数中可以按以下方式实现价格排序和直营优先排序的结合:
```javascript
sortPrice() {
if (this.value == 0) {
// 按商品ID排序
this.goodsList = this.goodsList.sort((a, b) => {
return a.id - b.id;
});
} else if (this.value == 1) {
// 按价格从高到低排序
let directGoods = this.goodsList.filter(item => item.isDirect);
let notDirectGoods = this.goodsList.filter(item => !item.isDirect);
directGoods.sort((a, b) => b.price - a.price);
notDirectGoods.sort((a, b) => b.price - a.price);
this.goodsList = directGoods.concat(notDirectGoods);
} else if (this.value == 2) {
// 按价格从低到高排序
let directGoods = this.goodsList.filter(item => item.isDirect);
let notDirectGoods = this.goodsList.filter(item => !item.isDirect);
directGoods.sort((a, b) => a.price - b.price);
notDirectGoods.sort((a, b) => a.price - b.price);
this.goodsList = directGoods.concat(notDirectGoods);
} else if (this.value == 3) {
// 按销量从高到低排序
this.goodsList = this.goodsList.sort((a, b) => {
return b.volume - a.volume;
});
} else if (this.value == 4) {
// 按销量从低到高排序
this.goodsList = this.goodsList.sort((a, b) => {
return a.volume - b.volume;
});
}
},
```
在 `isDirectGoods()` 函数中可以按以下方式实现价格排序和直营优先排序的结合:
```javascript
isDirectGoods() {
if (this.value == 1) {
// 直营优先,按价格从高到低排序
let directGoods = this.goodsList.filter(item => item.isDirect);
let notDirectGoods = this.goodsList.filter(item => !item.isDirect);
directGoods.sort((a, b) => b.price - a.price);
notDirectGoods.sort((a, b) => b.price - a.price);
this.goodsList = directGoods.concat(notDirectGoods);
} else if (this.value == 2) {
// 直营优先,按价格从低到高排序
let directGoods = this.goodsList.filter(item => item.isDirect);
let notDirectGoods = this.goodsList.filter(item => !item.isDirect);
directGoods.sort((a, b) => a.price - b.price);
notDirectGoods.sort((a, b) => a.price - b.price);
this.goodsList = directGoods.concat(notDirectGoods);
} else {
// 直营优先,按直营优先排序
this.goodsList = this.goodsList.sort((a, b) => {
return b.isDirect - a.isDirect;
});
}
}
```
这样,就可以实现在点击其他筛选条件的时候还能进行价格由高到低或由低到高的排序,同时也能够实现直营优先的排序。
阅读全文