麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 課堂 > 小程序 > 正文

微信小程序?qū)崿F(xiàn)商品屬性聯(lián)動(dòng)選擇

2020-03-21 16:01:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了微信小程序實(shí)現(xiàn)商品屬性聯(lián)動(dòng)選擇的具體代碼,供大家參考,具體內(nèi)容如下

效果演示:

微信小程序,商品,聯(lián)動(dòng)選擇

代碼示例

1、commodity.xml

<!-- <view class="title">屬性值聯(lián)動(dòng)選擇</view>  --><!--options--><view class="commodity_attr_list">   <!--每組屬性-->  <view class="attr_box" wx:for="{{attrValueList}}" wx:for-item="attrValueObj" wx:for-index="attrIndex">    <!--屬性名-->   <view class="attr_name">{{attrValueObj.attrKey}}</view>     <!--屬性值-->   <view class="attr_value_box">      <!--每個(gè)屬性值-->     <view class="attr_value {{attrIndex==firstIndex || attrValueObj.attrValueStatus[valueIndex]?(value==attrValueObj.selectedValue?'attr_value_active':''):'attr_value_disabled'}}" bindtap="selectAttrValue" data-status="{{attrValueObj.attrValueStatus[valueIndex]}}"     data-value="{{value}}" data-key="{{attrValueObj.attrKey}}" data-code="{{attrCode}}" data-index="{{attrIndex}}" data-selectedvalue="{{attrValueObj.selectedValue}}" wx:for="{{attrValueObj.attrValues}}" wx:for-item="value" wx:for-index="valueIndex">{{value}}</view>    </view>  </view> </view> <!--button--><view class="weui-btn-area">   <button class="weui-btn" bindtap="submit">選好了    </button> </view> 

上述代碼把頁(yè)面盒子分為兩部分commodity_attr_list和weui-btn-area。 
commodity_attr_list:循環(huán)獲取商品的屬性名和相對(duì)應(yīng)的屬性值,并布局頁(yè)面。 
weui-btn-area:提交校驗(yàn)并獲取選中商品屬性結(jié)果。

2、commodity.js

數(shù)據(jù)結(jié)構(gòu)

//數(shù)據(jù)結(jié)構(gòu):以一組一組的數(shù)據(jù)來(lái)進(jìn)行設(shè)定   commodityAttr: [   {    priceId: 1,    price: 35.0,    "stock": 8,    "attrValueList": [     {      "attrKey": "規(guī)格:",      "attrValue": "+免費(fèi)配料",      "attrCode": "1001"     },     {      "attrKey": "甜度:",      "attrValue": "七分甜",      "attrCode": "2001"     },     {      "attrKey": "加料:",      "attrValue": "珍珠",      "attrCode": "3001"     },     {      "attrKey": "冰塊:",      "attrValue": "少冰",      "attrCode": "4001"     }    ]   },   {    priceId: 2,    price: 35.1,    "stock": 9,    "attrValueList": [     {      "attrKey": "規(guī)格:",      "attrValue": "+燕麥",      "attrCode": "1002"     },     {      "attrKey": "甜度:",      "attrValue": "五分甜",      "attrCode": "2002"     },     {      "attrKey": "加料:",      "attrValue": "椰果",      "attrCode": "3002"     },     {      "attrKey": "冰塊:",      "attrValue": "去冰",      "attrCode": "4002"     }    ]   },   {    priceId: 3,    price: 35.2,    "stock": 10,    "attrValueList": [     {      "attrKey": "規(guī)格:",      "attrValue": "+布丁",      "attrCode": "1003"     },     {      "attrKey": "甜度:",      "attrValue": "無(wú)糖",      "attrCode": "2003"     },     {      "attrKey": "加料:",      "attrValue": "仙草",      "attrCode": "3003"     },     {      "attrKey": "冰塊:",      "attrValue": "常溫",      "attrCode": "4003"     }    ]   },   {    priceId: 4,    price: 35.2,    "stock": 10,    "attrValueList": [     {      "attrKey": "規(guī)格:",      "attrValue": "再加一份奶霜",      "attrCode": "1004"     },     {      "attrKey": "甜度:",      "attrValue": "無(wú)糖",      "attrCode": "2003"     },     {      "attrKey": "加料:",      "attrValue": "仙草",      "attrCode": "3004"     },     {      "attrKey": "冰塊:",      "attrValue": "熱飲",      "attrCode": "4004"     }    ]   },   {    priceId: 5,    price: 35.2,    "stock": 10,    "attrValueList": [     {      "attrKey": "規(guī)格:",      "attrValue": "+免費(fèi)配料",      "attrCode": "1004"     },     {      "attrKey": "甜度:",      "attrValue": "五分甜",      "attrCode": "2003"     },     {      "attrKey": "加料:",      "attrValue": "椰果",      "attrCode": "3004"     },     {      "attrKey": "冰塊:",      "attrValue": "常溫",      "attrCode": "4004"     }    ]   }  ],  attrValueList: [] }

屬性選中和取消選擇效果處理

onShow: function () {  this.setData({   includeGroup: this.data.commodityAttr  });  this.distachAttrValue(this.data.commodityAttr);  // 只有一個(gè)屬性組合的時(shí)候默認(rèn)選中   // console.log(this.data.attrValueList);   if (this.data.commodityAttr.length == 1) {   for (var i = 0; i < this.data.commodityAttr[0].attrValueList.length; i++) {    this.data.attrValueList[i].selectedValue = this.data.commodityAttr[0].attrValueList[i].attrValue;   }   this.setData({    attrValueList: this.data.attrValueList   });  } }, /* 獲取數(shù)據(jù) */ distachAttrValue: function (commodityAttr) {  /**   將后臺(tái)返回的數(shù)據(jù)組合成類似   {   attrKey:'型號(hào)',   attrValueList:['1','2','3']   }   */  // 把數(shù)據(jù)對(duì)象的數(shù)據(jù)(視圖使用),寫到局部?jī)?nèi)   var attrValueList = this.data.attrValueList;  // 遍歷獲取的數(shù)據(jù)   for (var i = 0; i < commodityAttr.length; i++) {   for (var j = 0; j < commodityAttr[i].attrValueList.length; j++) {    var attrIndex = this.getAttrIndex(commodityAttr[i].attrValueList[j].attrKey, attrValueList);    // console.log('屬性索引', attrIndex);     // 如果還沒(méi)有屬性索引為-1,此時(shí)新增屬性并設(shè)置屬性值數(shù)組的第一個(gè)值;索引大于等于0,表示已存在的屬性名的位置     if (attrIndex >= 0) {     // 如果屬性值數(shù)組中沒(méi)有該值,push新值;否則不處理      if (!this.isValueExist(commodityAttr[i].attrValueList[j].attrValue, attrValueList[attrIndex].attrValues)) {      attrValueList[attrIndex].attrValues.push(commodityAttr[i].attrValueList[j].attrValue);     }    } else {     attrValueList.push({      attrKey: commodityAttr[i].attrValueList[j].attrKey,      attrValues: [commodityAttr[i].attrValueList[j].attrValue]     });    }   }  }  // console.log('result', attrValueList)   for (var i = 0; i < attrValueList.length; i++) {   for (var j = 0; j < attrValueList[i].attrValues.length; j++) {    if (attrValueList[i].attrValueStatus) {     attrValueList[i].attrValueStatus[j] = true;    } else {     attrValueList[i].attrValueStatus = [];     attrValueList[i].attrValueStatus[j] = true;    }   }  }  this.setData({   attrValueList: attrValueList  }); }, getAttrIndex: function (attrName, attrValueList) {  // 判斷數(shù)組中的attrKey是否有該屬性值   for (var i = 0; i < attrValueList.length; i++) {   if (attrName == attrValueList[i].attrKey) {    break;   }  }  return i < attrValueList.length ? i : -1; }, isValueExist: function (value, valueArr) {  // 判斷是否已有屬性值   for (var i = 0; i < valueArr.length; i++) {   if (valueArr[i] == value) {    break;   }  }  return i < valueArr.length; }, /* 選擇屬性值事件 */ selectAttrValue: function (e) {  /*   點(diǎn)選屬性值,聯(lián)動(dòng)判斷其他屬性值是否可選   {   attrKey:'型號(hào)',   attrValueList:['1','2','3'],   selectedValue:'1',   attrValueStatus:[true,true,true]   }   console.log(e.currentTarget.dataset);   */  var attrValueList = this.data.attrValueList;  var index = e.currentTarget.dataset.index;//屬性索引   var key = e.currentTarget.dataset.key;  var value = e.currentTarget.dataset.value;  if (e.currentTarget.dataset.status || index == this.data.firstIndex) {   if (e.currentTarget.dataset.selectedvalue == e.currentTarget.dataset.value) {    // 取消選中     this.disSelectValue(attrValueList, index, key, value);   } else {    // 選中     this.selectValue(attrValueList, index, key, value);   }   } }, /* 選中 */ selectValue: function (attrValueList, index, key, value, unselectStatus) {  // console.log('firstIndex', this.data.firstIndex);   var includeGroup = [];  if (index == this.data.firstIndex && !unselectStatus) { // 如果是第一個(gè)選中的屬性值,則該屬性所有值可選    var commodityAttr = this.data.commodityAttr;   // 其他選中的屬性值全都置空    // console.log('其他選中的屬性值全都置空', index, this.data.firstIndex, !unselectStatus);    for (var i = 0; i < attrValueList.length; i++) {    for (var j = 0; j < attrValueList[i].attrValues.length; j++) {     attrValueList[i].selectedValue = '';    }   }  } else {   var commodityAttr = this.data.includeGroup;  }   // console.log('選中', commodityAttr, index, key, value);   for (var i = 0; i < commodityAttr.length; i++) {   for (var j = 0; j < commodityAttr[i].attrValueList.length; j++) {    if (commodityAttr[i].attrValueList[j].attrKey == key && commodityAttr[i].attrValueList[j].attrValue == value) {     includeGroup.push(commodityAttr[i]);    }   }  }  attrValueList[index].selectedValue = value;   // 判斷屬性是否可選   for (var i = 0; i < attrValueList.length; i++) {   for (var j = 0; j < attrValueList[i].attrValues.length; j++) {    attrValueList[i].attrValueStatus[j] = false;   }  }  for (var k = 0; k < attrValueList.length; k++) {   for (var i = 0; i < includeGroup.length; i++) {    for (var j = 0; j < includeGroup[i].attrValueList.length; j++) {     if (attrValueList[k].attrKey == includeGroup[i].attrValueList[j].attrKey) {      for (var m = 0; m < attrValueList[k].attrValues.length; m++) {       if (attrValueList[k].attrValues[m] == includeGroup[i].attrValueList[j].attrValue) {        attrValueList[k].attrValueStatus[m] = true;       }      }     }    }   }  }  // console.log('結(jié)果', attrValueList);   this.setData({   attrValueList: attrValueList,   includeGroup: includeGroup  });   var count = 0;  for (var i = 0; i < attrValueList.length; i++) {   for (var j = 0; j < attrValueList[i].attrValues.length; j++) {    if (attrValueList[i].selectedValue) {     count++;     break;    }   }  }  if (count < 2) {// 第一次選中,同屬性的值都可選    this.setData({    firstIndex: index   });  } else {   this.setData({    firstIndex: -1   });  } }, /* 取消選中 */ disSelectValue: function (attrValueList, index, key, value) {  var commodityAttr = this.data.commodityAttr;  attrValueList[index].selectedValue = '';   // 判斷屬性是否可選   for (var i = 0; i < attrValueList.length; i++) {   for (var j = 0; j < attrValueList[i].attrValues.length; j++) {    attrValueList[i].attrValueStatus[j] = true;   }  }  this.setData({   includeGroup: commodityAttr,   attrValueList: attrValueList  });   for (var i = 0; i < attrValueList.length; i++) {   if (attrValueList[i].selectedValue) {    this.selectValue(attrValueList, i, attrValueList[i].attrKey, attrValueList[i].selectedValue, true);   }  } }

結(jié)果提交

submit: function () {  var value = [];  for (var i = 0; i < this.data.attrValueList.length; i++) {   if (!this.data.attrValueList[i].selectedValue) {    break;   }   value.push(this.data.attrValueList[i].selectedValue);  }  if (i < this.data.attrValueList.length) {   wx.showToast({    title: '請(qǐng)選擇完整!',    icon: 'loading',    duration: 1000   })  } else {   var valueStr = "";   for(var i = 0;i < value.length;i++){    console.log(value[i]);    valueStr += value[i]+",";   }   wx.showModal({    title: '提示',    content: valueStr,    success: function (res) {     if (res.confirm) {      console.log('用戶點(diǎn)擊確定')     } else if (res.cancel) {      console.log('用戶點(diǎn)擊取消')     }    }   })    console.log(valueStr);  } }

3、commodity.wxss

.title {  padding: 10rpx 20rpx;  margin: 10rpx 0;  border-left: 4rpx solid #ccc; }  /*全部屬性的主盒子*/.commodity_attr_list {  background: #fff;  padding: 0 20rpx;  font-size: 26rpx;  overflow: hidden;  width: 100%; } /*每組屬性的主盒子*/.attr_box {  width: 100%;  overflow: hidden;  border-bottom: 1rpx solid #ececec;  display: flex; flex-direction: column;} /*屬性名*/.attr_name {  width: 20%;  float: left;  padding: 15rpx 0; } /*屬性值*/.attr_value_box {  width: 80%;  float: left;  padding: 15rpx 0;  overflow: hidden; } /*每個(gè)屬性值*/.attr_value {  float: left;  padding: 0 30rpx;  margin: 10rpx 10rpx;  border: 1rpx solid #ececec;  border-radius:5px; line-height:30px;} /*每個(gè)屬性選中的當(dāng)前樣式*/.attr_value_active {  border-radius: 10rpx;  color: #0089dc;  padding: 0 30rpx;  border: 1rpx solid #0089dc;  /* background: #fff;  */} /*禁用屬性*/.attr_value_disabled {  color: #ccc; }  /*button*/.weui-btn-area {  margin: 1.17647059em 15px 0.3em; } .weui-btn{ width: 80%; height: 100rpx; border-radius: 3rpx;  background-color:#0089dc;  color: #fff; }

好了,復(fù)制上述代碼就可以實(shí)現(xiàn)效果哦,趕緊試試吧!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 毛片免费大全短视频 | 久久久久久久免费看 | 欧美 日韩 中文 | 中文字幕极速在线观看 | 美女视频黄a视频免费全过程 | 欧洲成人一区 | 亚洲国产高清视频 | 黄色免费在线电影 | 久久精品9 | 视频一区二区三区在线播放 | 国产一区二区三区手机在线 | av成人在线电影 | 成人国产精品久久久 | 羞羞电影在线观看www | 国产精品伊人久久 | 国产91中文字幕 | 91精品国产九九九久久久亚洲 | 精品久久久久久久久久久久久 | 亚洲视频在线视频 | 国产手机在线视频 | 日本黄色一级视频 | 久久久久亚洲视频 | 九九热视频免费在线观看 | 色综合视频网 | 欧美一级高清免费 | 玖草在线资源 | 国产在线看一区 | 国产亚洲精品久久久久久大师 | 国产午夜三级一区二区三桃花影视 | v片在线看| 国产一级一国产一级毛片 | 久久免费视屏 | 把娇妻调教成暴露狂 | 久久综合久久精品 | 91精品国产91久久久 | 国产伦久视频免费观看视频 | 伊人一二三四区 | 草妞视频| 一级一级一级一级毛片 | 欧美18videos性处按摩 | 国产a级片电影 |