这里开始js逻辑代码了,一开始是手动让用户去添加属性规格,后来项目经理又让改成用户进来就是默认选中所有规格的第一个属性,下面用代码展示遇到的坑吧
page({ data:{ values:[],//这是定义好的空数组 } /** * 规格选择弹出框隐藏 */ closePopupTap: function () { console.log(this.data.goodList) this.setData({ hideShopPopup: true }) }, //数量减 numJianTap: function () { if (this.data.buyNumber > this.data.buyNumMin) { var currentNum = this.data.buyNumber; currentNum--; this.setData({ buyNumber: currentNum }) } }, //数量加 numJiaTap: function () { if (this.data.buyNumber < this.data.buyNumMax) { var currentNum = this.data.buyNumber; currentNum++; this.setData({ buyNumber: currentNum }) } }, /** * 选择商品规格 * @param {Object} e */ labelItemTap: function (e) { console.log(e,'规格当前点击的状态') var that = this; // 取消该分类下的子栏目所有的选中状态 var childs = that.data.goodList.showsku[e.currentTarget.dataset.propertyindex].values; console.log(that.data.values,childs,'选中的规格属性',that.data.goodList) for (var i = 0; i < childs.length; i++) { that.data.goodList.showsku[e.currentTarget.dataset.propertyindex].values[i].active = false; } // // 设置当前选中状态 that.data.goodList.showsku[e.currentTarget.dataset.propertyindex].values[e.currentTarget.dataset.propertychildindex].active = true; console.log("hahahahhahaha",that.data.goodList) //获取当前选中状态id that.data.values[e.currentTarget.dataset.propertyindex] = that.data.goodList.showsku[e.currentTarget.dataset.propertyindex].values[e.currentTarget.dataset.propertychildindex].id; console.log(that.data.values); var skushowpricekey = ""; for (var p = 0; p < that.data.values.length; p++) { if(that.data.values[p]){ if(p==that.data.values.length-1){ skushowpricekey+= that.data.values[p]; }else{ skushowpricekey+= that.data.values[p]+","; } } } console.log(skushowpricekey) for(var key in that.data.goodList.skulist){ var skushownewprice = that.data.goodList.skulist[key].newprice; var skushowstock = that.data.goodList.skulist[key].stock; var skushowprice = that.data.goodList.skulist[key].price; if(key==skushowpricekey){ that.setData({ skushowprice : skushowprice,//原价 skushowstock : skushowstock, //库存 skushownewprice: skushownewprice, //现价 skushowpricekey:skushowpricekey,//选中的商品规格ID }) // console.log(that.data.skushowprice) } } // 获取所有的选中规格尺寸数据 var needSelectNum = that.data.goodList.showsku.length; var curSelectNum = 0; var propertyChildIds = ""; var propertyChildNames = ""; for (var i = 0; i < that.data.goodList.showsku.length; i++) { childs = that.data.goodList.showsku[i].values; for (var j = 0; j < childs.length; j++) { if (childs[j].active) { curSelectNum++; propertyChildIds = propertyChildIds + that.data.goodList.showsku[i].id + ":" + childs[j].id + ","; propertyChildNames = propertyChildNames + that.data.goodList.showsku[i].name + ":" + childs[j].name + " "; } } } //判断是否选中所有规格 var canSubmit = false; if (needSelectNum == curSelectNum) { canSubmit = true; } that.setData({ values:that.data.values,//保存价格和库存 goodList:that.data.goodList, canSubmit:canSubmit,//判断全部选中规格没有 }) }, // 默认选中的商品规则 clickItemTap:function(){ var that = this; var childs = that.data.goodList.showsku; var skushowpricekey = ""; for (var i = 0; i < childs.length; i++) { childs[i].values[0].active = true; //就是在这一部踩的坑,前面的逻辑全都写出来了,然后当时想的是因为需要给后台传规格的属性id,id又都是循环拼接出来的字符串,如果在点击事件里去重新拼接字符串会很麻烦,后来仔细屡了屡思路发现在全局data中已经定义好一个valus数组,这个数组不管你前后点击选中的数据都会从第一个属性id开始循环,这下问题就解决了,不管点击几次id始终都是重新排序去跟后台数据接口id对比(就是这段代码解决了所有问题,that.data.values[i] = childs[i].values[0].id;) that.data.values[i] = childs[i].values[0].id; if(i==childs.length-1){ skushowpricekey+= childs[i].values[0].id; }else{ skushowpricekey+= childs[i].values[0].id +","; } } console.log(that.data.values,childs,'选中的规格属性',that.data.goodList) console.log(skushowpricekey,'我是id') for(var key in that.data.goodList.skulist){ var skushownewprice = that.data.goodList.skulist[key].newprice; var skushowstock = that.data.goodList.skulist[key].stock; var skushowprice = that.data.goodList.skulist[key].price; if(key==skushowpricekey){ that.setData({ skushowprice : skushowprice,//原价 skushowstock : skushowstock, //库存 skushownewprice: skushownewprice, //现价 skushowpricekey: skushowpricekey,//选中的商品规格ID }) // console.log(that.data.skushowprice) } } // 获取所有的选中规格尺寸数据 var needSelectNum = that.data.goodList.showsku.length; var curSelectNum = 0; for (var i = 0; i < that.data.goodList.showsku.length; i++) { childs = that.data.goodList.showsku[i].values; for (var j = 0; j < childs.length; j++) { if (childs[j].active) { curSelectNum++; } } } //判断是否选中所有规格 var canSubmit = false; if (needSelectNum == curSelectNum) { canSubmit = true; } that.setData({ goodList:that.data.goodList, canSubmit:canSubmit,//判断全部选中规格没有 }) }, })