欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序实现商品属性联动选择

程序员文章站 2023-11-16 18:48:16
本文实例为大家分享了微信小程序实现商品属性联动选择的具体代码,供大家参考,具体内容如下 效果演示: 代码示例 1、commodity.xml <...

本文实例为大家分享了微信小程序实现商品属性联动选择的具体代码,供大家参考,具体内容如下

效果演示:

微信小程序实现商品属性联动选择

代码示例

1、commodity.xml

<!-- <view class="title">属性值联动选择</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:循环获取商品的属性名和相对应的属性值,并布局页面。
weui-btn-area:提交校验并获取选中商品属性结果。

2、commodity.js

数据结构

//数据结构:以一组一组的数据来进行设定 
  commodityattr: [
   {
    priceid: 1,
    price: 35.0,
    "stock": 8,
    "attrvaluelist": [
     {
      "attrkey": "规格:",
      "attrvalue": "+免费配料",
      "attrcode": "1001"
     },
     {
      "attrkey": "甜度:",
      "attrvalue": "七分甜",
      "attrcode": "2001"
     },
     {
      "attrkey": "加料:",
      "attrvalue": "珍珠",
      "attrcode": "3001"
     },
     {
      "attrkey": "冰块:",
      "attrvalue": "少冰",
      "attrcode": "4001"
     }
    ]
   },
   {
    priceid: 2,
    price: 35.1,
    "stock": 9,
    "attrvaluelist": [
     {
      "attrkey": "规格:",
      "attrvalue": "+燕麦",
      "attrcode": "1002"
     },
     {
      "attrkey": "甜度:",
      "attrvalue": "五分甜",
      "attrcode": "2002"
     },
     {
      "attrkey": "加料:",
      "attrvalue": "椰果",
      "attrcode": "3002"
     },
     {
      "attrkey": "冰块:",
      "attrvalue": "去冰",
      "attrcode": "4002"
     }
    ]
   },
   {
    priceid: 3,
    price: 35.2,
    "stock": 10,
    "attrvaluelist": [
     {
      "attrkey": "规格:",
      "attrvalue": "+布丁",
      "attrcode": "1003"
     },
     {
      "attrkey": "甜度:",
      "attrvalue": "无糖",
      "attrcode": "2003"
     },
     {
      "attrkey": "加料:",
      "attrvalue": "仙草",
      "attrcode": "3003"
     },
     {
      "attrkey": "冰块:",
      "attrvalue": "常温",
      "attrcode": "4003"
     }
    ]
   },
   {
    priceid: 4,
    price: 35.2,
    "stock": 10,
    "attrvaluelist": [
     {
      "attrkey": "规格:",
      "attrvalue": "再加一份奶霜",
      "attrcode": "1004"
     },
     {
      "attrkey": "甜度:",
      "attrvalue": "无糖",
      "attrcode": "2003"
     },
     {
      "attrkey": "加料:",
      "attrvalue": "仙草",
      "attrcode": "3004"
     },
     {
      "attrkey": "冰块:",
      "attrvalue": "热饮",
      "attrcode": "4004"
     }
    ]
   },
   {
    priceid: 5,
    price: 35.2,
    "stock": 10,
    "attrvaluelist": [
     {
      "attrkey": "规格:",
      "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
   });
  }
 },
 /* 获取数据 */
 distachattrvalue: function (commodityattr) {
  /** 
  将后台返回的数据组合成类似 
  { 
  attrkey:'型号', 
  attrvaluelist:['1','2','3'] 
  } 
  */
  // 把数据对象的数据(视图使用),写到局部内 
  var attrvaluelist = this.data.attrvaluelist;
  // 遍历获取的数据 
  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,此时新增属性并设置属性值数组的第一个值;索引大于等于0,表示已存在的属性名的位置 
    if (attrindex >= 0) {
     // 如果属性值数组中没有该值,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) {
  // 判断数组中的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) {
  /* 
  点选属性值,联动判断其他属性值是否可选 
  { 
  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; 
}

好了,复制上述代码就可以实现效果哦,赶紧试试吧!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。