最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序實現(xiàn)商品屬性聯(lián)動選擇

 更新時間:2019年02月15日 11:47:29   作者:不川類庫  
這篇文章主要為大家詳細介紹了微信小程序實現(xiàn)商品屬性聯(lián)動選擇,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

效果演示:

代碼示例

1、commodity.xml

<!-- <view class="title">屬性值聯(lián)動選擇</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"> 
     <!--每個屬性值-->
     <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> 

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

2、commodity.js

數(shù)據(jù)結構

//數(shù)據(jù)結構:以一組一組的數(shù)據(jù)來進行設定 
  commodityAttr: [
   {
    priceId: 1,
    price: 35.0,
    "stock": 8,
    "attrValueList": [
     {
      "attrKey": "規(guī)格:",
      "attrValue": "+免費配料",
      "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": "無糖",
      "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": "無糖",
      "attrCode": "2003"
     },
     {
      "attrKey": "加料:",
      "attrValue": "仙草",
      "attrCode": "3004"
     },
     {
      "attrKey": "冰塊:",
      "attrValue": "熱飲",
      "attrCode": "4004"
     }
    ]
   },
   {
    priceId: 5,
    price: 35.2,
    "stock": 10,
    "attrValueList": [
     {
      "attrKey": "規(guī)格:",
      "attrValue": "+免費配料",
      "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);
  // 只有一個屬性組合的時候默認選中 
  // 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) {
  /** 
  將后臺返回的數(shù)據(jù)組合成類似 
  { 
  attrKey:'型號', 
  attrValueList:['1','2','3'] 
  } 
  */
  // 把數(shù)據(jù)對象的數(shù)據(jù)(視圖使用),寫到局部內 
  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); 
    // 如果還沒有屬性索引為-1,此時新增屬性并設置屬性值數(shù)組的第一個值;索引大于等于0,表示已存在的屬性名的位置 
    if (attrIndex >= 0) {
     // 如果屬性值數(shù)組中沒有該值,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) {
  /* 
  點選屬性值,聯(lián)動判斷其他屬性值是否可選 
  { 
  attrKey:'型號', 
  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) { // 如果是第一個選中的屬性值,則該屬性所有值可選 
   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('結果', 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);
   }
  }
 }

結果提交

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: '請選擇完整!',
    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('用戶點擊確定')
     } else if (res.cancel) {
      console.log('用戶點擊取消')
     }
    }
   }) 
   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; 
} 
/*每個屬性值*/
.attr_value { 
 float: left; 
 padding: 0 30rpx; 
 margin: 10rpx 10rpx; 
 border: 1rpx solid #ececec; 
 border-radius:5px;
 line-height:30px;
} 
/*每個屬性選中的當前樣式*/
.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; 
}

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • JavaScript中解決多瀏覽器兼容性23個問題的快速解決方法

    JavaScript中解決多瀏覽器兼容性23個問題的快速解決方法

    下面小編就為大家?guī)硪黄狫avaScript中解決多瀏覽器兼容性23個問題的快速解決方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • js實現(xiàn)簡易ATM功能

    js實現(xiàn)簡易ATM功能

    這篇文章主要為大家詳細介紹了js實現(xiàn)簡易ATM功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • JAVASCRIPT車架號識別/驗證函數(shù)代碼 汽車車架號驗證程序

    JAVASCRIPT車架號識別/驗證函數(shù)代碼 汽車車架號驗證程序

    偶然中在CSDN里找到C#版的驗證程序,因此改編了一版JS版本,相信會對大家有用
    2012-01-01
  • 原生js頁面滾動延遲加載圖片

    原生js頁面滾動延遲加載圖片

    這篇文章主要介紹了原生js頁面滾動延遲加載圖片的相關資料,現(xiàn)在瀑布流效果大行其道,各種網(wǎng)站都有應用,尤其是專業(yè)的圖片類型的網(wǎng)站,感興趣的朋友可以參考下
    2015-12-12
  • 詳解webpack運行Babel教程

    詳解webpack運行Babel教程

    這篇文章主要介紹了詳解webpack運行Babel教程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • JS中parseInt()和map()用法分析

    JS中parseInt()和map()用法分析

    這篇文章主要介紹了JS中parseInt()和map()用法,結合實例形式分析了parseInt()和map()方法的功能、參數(shù)、具體用法與相關注意事項,需要的朋友可以參考下
    2016-12-12
  • 關于Javascript回調函數(shù)的一個妙用

    關于Javascript回調函數(shù)的一個妙用

    相信在剛開始學習JavaScript的時候,很多人感到最困惑的就是回調函數(shù)了。本文通過一個小小的例子來分析回調函數(shù)的用法。對大家學習Javascript回調函數(shù)很有幫助,有需要的可以來參考學習。
    2016-08-08
  • javascript一些實用技巧小結

    javascript一些實用技巧小結

    javascript一些實用技巧小結,學習js的朋友可以看下。
    2011-03-03
  • JS實現(xiàn)線性表的鏈式表示方法示例【經典數(shù)據(jù)結構】

    JS實現(xiàn)線性表的鏈式表示方法示例【經典數(shù)據(jù)結構】

    這篇文章主要介紹了JS實現(xiàn)線性表的鏈式表示方法,簡單講解了線性表鏈式表示的原理并結合實例形式分析了js針對線性表鏈式表示的創(chuàng)建、插入、刪除等節(jié)點操作技巧,需要的朋友可以參考下
    2017-04-04
  • JS表格組件神器bootstrap table詳解(強化版)

    JS表格組件神器bootstrap table詳解(強化版)

    這篇文章主要以實例的方式為大家再次介紹了JS表格組件神器bootstrap table,bootstrap table界面采用扁平化的風格,用戶體驗比較好,更好兼容各種客戶端,需要了解更多bootstrap table的朋友可以參考下
    2016-05-05

最新評論

宁强县| 右玉县| 施甸县| 鄂州市| 交城县| 宁化县| 鸡泽县| 香格里拉县| 额尔古纳市| 繁昌县| 句容市| 安徽省| 江川县| 英吉沙县| 格尔木市| 贵定县| 谷城县| 凤阳县| 贵定县| 枣阳市| 安仁县| 兴国县| 南宫市| 百色市| 论坛| 彰化县| 文安县| 通城县| 漳州市| 屏南县| 鹤壁市| 如皋市| 岳普湖县| 廉江市| 太仓市| 巴中市| 南阳市| 扎兰屯市| 彰化市| 图们市| 松阳县|