久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

js實現(xiàn)購物車計算的方法

瀏覽:156日期:2024-05-31 11:51:53

本文實例為大家分享了js實現(xiàn)購物車計算的具體代碼,供大家參考,具體內(nèi)容如下

以店鋪為單位,每個店鋪下面有對應(yīng)的商品選項,可全選,商品單選,店鋪單選,商品數(shù)量加減,刪除

js實現(xiàn)購物車計算的方法

代碼:

<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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 文登市| 大城县| 潼南县| 松桃| 呼玛县| 伊川县| 南乐县| 肃北| 武宁县| 龙游县| 巴中市| 独山县| 元朗区| 定远县| 金门县| 沅江市| 永新县| 娱乐| 迁安市| 田林县| 营口市| 韶关市| 绥江县| 永清县| 汝城县| 西安市| 光山县| 慈利县| 武定县| 渭南市| 鄯善县| 竹山县| 马鞍山市| 营山县| 义乌市| 吉林市| 鲁山县| 隆德县| 汾西县| 新建县| 石泉县|