uni-app购物车-价格、数量计算
uni-app价格计算uni-app购物车uni-app购物车价格计算
·
1,购物车所有已勾选商品的总价、总数计算
2,单个商品的总价、总数计算
// carArr是购物车商品列表
computed: {
// 已勾选所有商品的总价和总数计算:
// 总价
totalPrice () {
let totalP = 0
this.carArr.map(item => {
return item.checked? totalP += Number(item.price) : totalP += 0
})
return totalP.toFixed(2)
},
// 总数
totalNum () {
let totalP = 0
this.carArr.map(item => {
return item.checked? totalP += item.num : totalP += 0
})
return totalP.toFixed(0)
},
},
methods: {
// 单个商品的总价和总数计算:
// 单个商品+
add (item) {
for (var j = 0; j < this.carArr.length; j++) {
var items = this.carArr[j]
if (item.id == items.id) {
items.num ++
items.allP = items.num* items.price
}
}
},
// 单个商品-
cut (item) {
for (var j = 0; j < this.carArr.length; j++) {
var items = this.carArr[j]
if (item.id == items.id) {
items.num > 1 ? items.num -- : uni.showToast({title: '哎哟,只剩一件啦!', duration: 2000});
items.allP = items.num* items.price
}
}
}
}
更多推荐
已为社区贡献11条内容
所有评论(0)