商品多规格下单处理数据

在这里插入图片描述多商品规格下单,选择规格切换对应规格的价格和库存。
在获取商品详情之后处理数据

        let goodsSpecList = res.data.item_prop //商品规格
        let goodsSpecPrice = res.data.item_spec //不同规格对应的价格
        let priceKey = []
        let specname = []
        for (let i = 0; i < goodsSpecList.length; i++) {
          priceKey.push(goodsSpecList[i].option[0].code)
          specname.push(goodsSpecList[i].option[0].option)
        }
        this.setData({
          goodsSpecList: goodsSpecList,
          goodsSpecPrice: goodsSpecPrice,
          specname: specname,
          priceKey: priceKey,
          specId: priceKey.join('-'), //向接口传的规格
          list: res.data,
        })
        for (let i = 0; i < goodsSpecPrice.length; i++) { //获取价格
          if (goodsSpecPrice[i].spc_code === this.data.specId) {
            this.setData({
              specPrice: goodsSpecPrice[i].price
            })
          }
        }
**选择规格:**
  chooseSpec(e) {
    let priceKey = this.data.priceKey
    let specname = this.data.specname
    let index = e.currentTarget.dataset.idx
    let itemId = e.currentTarget.dataset.itemid
    priceKey.splice(index, 1, itemId)
    this.setData({
      itemId: itemId,
      specname: specname,
      priceKey: priceKey,
      specId: priceKey.join('-'), //商品规格
      specname: specname
    })
    let goodsSpecPrice = this.data.goodsSpecPrice
    for (let i = 0; i < goodsSpecPrice.length; i++) {
      if (goodsSpecPrice[i].spc_code == this.data.specId) {
        this.setData({
          specPrice: goodsSpecPrice[i].price,
        })
      }
    }
  },
 **页面** 
选择规格:
       <view class="color_box" wx:for="{{goodsSpecList}}" wx:key="{{index}}">
        <view class="color_text">{{item.cname}}</view>
        <view class="color_list" wx:for-item="itn">
          <block wx:for="{{item.option}}" wx:for-item="sub" wx:for-index="idx" wx:key="{{index}}">
            <view bindtap='chooseSpec' data-idx='{{index}}' data-specname='{{sub.option}}' data-itemid='{{sub.code}}'
              class="{{priceKey[index]===sub.code?'origin':''}}">{{sub.option}}</view>
          </block>
        </view>
      </view>
价格:
    <block wx:for="{{goodsSpecPrice}}">
       <view wx:if="{{specId==item.spc_code}}">{{specPrice}}</view>
    </block>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值