小程序实现商品属性选择或规格选择

 4865

这篇文章主要为大家详细介绍了小程序实现商品属性选择或规格选择,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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


实现效果


6052e9a2482f8.png


1、wxml

  1. <view wx:for="{{list}}" wx:key="index" wx:key="index" wx:for-index="childIndex" style="margin: 40px 0">
  2.     <view>{{item.name}}</view> 
  3.  
  4.     <view class="s" wx:for="{{item.option_value}}" wx:key="index" >
  5.         <text class="{{indexArr[childIndex] == index ? 'active':''}}" bindtap="choice" data-fid="{{childIndex}}" data-id="{{index}}">
  6.             {{item.name}}
  7.         </text>
  8.     </view>
  9. </view>

2、js

  1. data: {
  2.     //数据
  3.     list: [
  4.         {
  5.             "goods_option_id": 1737,
  6.             "option_id": 1737,
  7.             "name": "冰度",
  8.             "option_value": [
  9.                 {
  10.                     "goods_option_value_id": 3606,
  11.                     "option_value_id": 3606,
  12.                     "name": "正常冰",
  13.                     "image": "xxxxxx.png"
  14.                 },
  15.                 {
  16.                     "goods_option_value_id": 3605,
  17.                     "option_value_id": 3605,
  18.                     "name": "少冰",
  19.                     "image": "xxxxxx.png"
  20.                 },
  21.                 {
  22.                     "goods_option_value_id": 3604,
  23.                     "option_value_id": 3604,
  24.                     "name": "热饮",
  25.                     "image": "xxxxxx.png"
  26.                 }
  27.             ]
  28.         },
  29.         {
  30.             "goods_option_id": 1738,
  31.             "option_id": 1738,
  32.             "name": "糖度",
  33.             "option_value": [
  34.                 {
  35.                     "goods_option_value_id": 3608,
  36.                     "option_value_id": 3608,
  37.                     "name": "正常糖",
  38.                     "image": "xxxxxx.png"
  39.                 },
  40.                 {
  41.                     "goods_option_value_id": 3607,
  42.                     "option_value_id": 3607,
  43.                     "name": "少糖",
  44.                     "image": "xxxxxx.png"
  45.                 }
  46.             ]
  47.         },
  48.         {
  49.             "goods_option_id": 1737,
  50.             "option_id": 1737,
  51.             "name": "冰度",
  52.             "option_value": [
  53.                 {
  54.                     "goods_option_value_id": 3606,
  55.                     "option_value_id": 3606,
  56.                     "name": "正常冰",
  57.                     "image": "xxxxxx.png"
  58.                 },
  59.                 {
  60.                     "goods_option_value_id": 3605,
  61.                     "option_value_id": 3605,
  62.                     "name": "少冰",
  63.                     "image": "xxxxxx.png"
  64.                 },
  65.                 {
  66.                     "goods_option_value_id": 3604,
  67.                     "option_value_id": 3604,
  68.                     "name": "热饮",
  69.                     "image": "xxxxxx.png"
  70.                 }
  71.             ]
  72.         }
  73.     ],
  74.     arr: [],
  75.     indexArr: []
  76. },
  77. choice(e) {
  78.     const fid = e.currentTarget.dataset.fid;
  79.     const id = e.currentTarget.dataset.id;
  80.     const arr = this.data.arr,
  81.     arr2 = this.data.indexArr;
  82.     
  83.     arr[fid] = this.data.list[fid].option_value[id].name;
  84.     arr2[fid] = id;
  85.     this.setData({
  86.         arr: arr,
  87.         indexArr: arr2
  88.     })
  89. },
  90.  
  91. onLoad: function (options) {
  92.     const res = this.data.indexArr;
  93.     this.data.list.forEach((e,i) => {
  94.         res[i] = 0;
  95.         this.setData({
  96.             indexArr: res
  97.         })
  98.     });
  99. }



本文网址:https://www.zztuku.com/index.php/detail-8738.html
站长图库 - 小程序实现商品属性选择或规格选择
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐

    Vue 手势组件教程