主要逻辑
checkAll: {
get() {
return this.products.every(item => item.isSelected);
},
set(val) {
this.products.forEach(item => (item.isSelected = val));
}
},
<template>
<div class="content">
<h2>购物车</h2>
<van-swipe :autoplay="3000" indicator-color="white">
<van-swipe-item v-for="(item,i) of products" :key="i">
<img class="swiper" :src="item.productCover" alt @click="toggle(i)" />
</van-swipe-item>
</van-swipe>
<div class="item" v-for="item of products" :key="item.id">
<van-checkbox class="checkAll" v-model="item.isSelected"></van-checkbox>
<van-stepper
class="num"
v-model="item.productCount"
input-width="20px"
button-size="20px"
integer
/>
<van-card
:price="item.productPrice"
:desc="item.productInfo"
:title="item.productName"
:thumb="item.productCover"
>
<div slot="bottom">小计:{{item.productPrice*item.productCount | money(2)}}</div>
<div slot="footer">
<van-button size="mini" type="danger" @click="handledelete(item)">删除</van-button>
</div>
</van-card>
</div>
<van-submit-bar :price="sum" button-text="提交订单">
<van-checkbox v-model="checkAll">全选</van-checkbox>
</van-submit-bar>
</div>
</template>
<script>
import { Dialog } from 'vant';
import { ImagePreview } from "vant";
export default {
name: "cart",
data() {
return {
products: [],
img: []
};
},
mounted() {
this.axios.get("http://yapi.demo.qunar.com/mock/34614/").then(res => {
this.products = res.data;
this.products.forEach(item => {
this.img.push(item.productCover);
});
});
},
computed: {
checkAll: {
get() {
return this.products.every(item => item.isSelected);
},
set(val) {
this.products.forEach(item => (item.isSelected = val));
}
},
sum() {
var total = 0;
for (let i = 0; i < this.products.length; i++) {
if (this.products[i].isSelected) {
total =
total +
this.products[i].productCount * this.products[i].productPrice;
} else {
continue;
}
}
return total * 100;
}
},
methods: {
handledelete(item) {
Dialog.confirm({
title: "提示",
message: "确认删除"
})
.then(() => {
var products = this.products.filter(value => value !== item);
this.products = products;
})
.catch(() => {
// on cancel
});
},
toggle(index) {
ImagePreview({
images: this.img,
startPosition: index
});
}
}
};
</script>