js實現(xiàn)購物車計算的方法
更新時間:2021年08月18日 11:44:14 作者:qq_43561241
這篇文章主要為大家詳細介紹了js實現(xiàn)購物車的計算方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js實現(xiàn)購物車計算的具體代碼,供大家參考,具體內容如下
以店鋪為單位,每個店鋪下面有對應的商品選項,可全選,商品單選,店鋪單選,商品數(shù)量加減,刪除

代碼:
<script>
export default {
data() {
return {
count:0, //所有商品的數(shù)量和
shopCarList: [], //所有商品
selectList:[] //已選擇的商品
}
},
computed: {
//選擇的合計價格
selectPrice(){
return this.selectList.reduce((total,item,index)=>{
return total+item.quantity*(item.activityPrice || item.price);
},0)
},
//選擇的合計數(shù)量
selectNum(){
return this.selectList.reduce((total,item,index)=>{
return total+item.quantity;
},0)
},
//是否全選
isSelecetAll(){
return this.shopCarList.every(item=>{
return item.shopSelect===true
})
}
},
methods: {
//增加
increment(itm){
itm.quantity<itm.stock?itm.quantity++:'';
},
//減少
decrement(itm){
itm.quantity>1?itm.quantity--:''
},
//全選點擊
allChooseHandle(){
this.initSelectAll(!this.isSelecetAll);
},
//商品單選點擊
productChooseHandle(itm,index){
let productFlag = !itm.productSelect;
this.$set(itm,'productSelect',productFlag);
let shopFlag = this.shopCarList[index].products.every(i=>{
return i.productSelect===true;
})
this.$set(this.shopCarList[index],'shopSelect',shopFlag);
this.getSelected();
},
//店鋪單選點擊
shopChooseHandle(item,index){
let Flag = !item.shopSelect;
this.$set(item,'shopSelect',Flag);
item.products.forEach((itm,idx)=>{
this.$set(itm,'productSelect',Flag);
})
this.getSelected();
},
//獲取已選商品
getSelected(){
let tempArr = [];
this.shopCarList.forEach((item,index)=>{
item.products.forEach((itm,idx)=>{
if(itm.productSelect){
tempArr.push(itm);
}
})
})
this.selectList = tempArr;
},
//是否全選初始化
initSelectAll(Flag){
this.shopCarList.forEach((item,index)=>{
this.$set(item,'shopSelect',Flag);
item.products.forEach((itm,idx)=>{
this.$set(itm,'productSelect',Flag);
})
})
this.getSelected();
},
//獲取購物車列表
getCarList(){
return this.$api.personal.getCarList().then(res=>{
if(res.result==='000'){
//過期商品數(shù)
let expiredNum = 0;
res.data.expiredList.forEach(item=>{
item.products.forEach(itm=>{
expiredNum += itm.quantity
})
})
//有效商品數(shù)量
this.count = res.data.count - expiredNum;
this.shopCarList = res.data.shopCarList;
return Promise.resolve()
}
})
},
//刪除點擊
delCarList(itm,index,idx){
this.$api.personal.delCarList( [itm.carId] ).then(res=>{
if(res.result==='000'){
this.count -= itm.quantity;
this.shopCarList[index].products.splice(idx,1);
if(this.shopCarList[index].products.length===0){
this.shopCarList.splice(index,1);
}
this.getSelected();
}
})
},
},
created() {
this.getCarList().then(()=>{
this.initSelectAll(true);
});
},
};
</script>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
JS中檢測數(shù)據(jù)類型的幾種方式及優(yōu)缺點小結
這篇文章主要介紹了JS中檢測數(shù)據(jù)類型的幾種方式及優(yōu)缺點小結,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12
微信/支付寶小程序實現(xiàn)彈窗動畫縮放到某個位置的示例代碼
本文詳細介紹了如何使用HTML、CSS和JavaScript實現(xiàn)動畫函數(shù),包括參數(shù)設置和動畫過程中的狀態(tài)管理,文章還涉及了如何獲取DOM元素、設置動畫開始和結束的回調函數(shù),感興趣的朋友跟隨小編一起看看吧2024-10-10
javascript實現(xiàn)點擊產(chǎn)生隨機圖形
這篇文章主要為大家詳細介紹了javascript實現(xiàn)點擊產(chǎn)生隨機圖形,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-01-01

