微信小程序嵌套循环+实现商品规格的选择

1.index.wxml

//properties是从接口获取到的
<!--数据-->
<view class="selectsize_content">
<block wx:for="{{properties}}" wx:key="item" wx:for-index="id">
 <view class="content_title">{{item.name}}</view>
  <view class='content_list'>          
      <block wx:for="{{item.childsCurGoods}}" wx:key="items">
           <text class="{{ item.isActive?'active':''}}" data-select-index="{{id}}"
          data-attr-index="{{index}}" data-content="{{properties}}"  data-proid="{{item.propertyId}}:{{item.id}}"
          bindtap='clickMenu'>{{item.name}}
        </text>
       </block>
   </view>
</block>
</view>
//商品规格
<van-card
  wx:if="{{showPeice}}"
  price="{{basicInfo.minPrice}}"
  title="{{basicInfo.name}}"
  thumb="{{ detailSwiper[0].pic }}"
/>
<van-card
  wx:if="{{ !showPeice}}"
  price="{{shangPrice}}"
  title="{{basicInfo.name}}"
  thumb="{{ detailSwiper[0].pic }}"
/>

在控制台打印一下,这是 properties的数据
在这里插入图片描述

2.index.js

import http from '../../../request/http'
import Toast from '../../../miniprogram_npm/vant-weapp/toast/toast';
Page({
  data:{
    userId:"",
    properties:[],//购买的数组
    str:"",
    shangPrice:"",//商品规格的price
    showPeice:true,
 },
 clickMenu: function (event) {
     let that = this
     let selectIndex = event.currentTarget.dataset.selectIndex; 
     let attrIndex = event.currentTarget.dataset.attrIndex;  
     let content = event.currentTarget.dataset.content;
     var count = content[selectIndex].childsCurGoods.length;
     for (var i = 0; i < count; i++) {
       content[selectIndex].childsCurGoods[i].isActive= false
     }
     content[selectIndex].childsCurGoods[attrIndex].isActive= true;
     // 必须重新渲染数据
     that.setData({ 
       properties: content
     })
    
     let proid = event.currentTarget.dataset.proid;  //55683:567318
     if(that.data.str ==""){
        that.data.str = proid
     }else{
       //判断重复     propertyId
       if(that.data.str.indexOf(proid.split(':')[0])==-1){  
            that.data.str+=','+proid
       }else{
          //如果重复,去替换掉
          let propertyId = proid.split(':')[0]+':';
          let reg = new RegExp(propertyId+"\\d{0,}")
          that.data.str = that.data.str.replace(reg,proid);
       }
      //  console.log(that.data.str)
     }
     //获取规格后的价钱
     http.getPrice({goodsId:that.data.userId,propertyChildIds:that.data.str}).then(res=>{
         if(res.data.code==0){  //code=0说明接口成功
            // console.log(res.data.data.price)
            that.setData({
              shangPrice:res.data.data.price
            })
         }else{
            Toast(res.data.msg);
         }
     })
   },
   
})

3.index.wxss

.selectsize_content{
  width:100%;
  height:500rpx;
}
.content_title{
  line-height:100rpx;
  margin-left:20rpx;
}
.content_list{
  line-height:100rpx;
  margin-left:20rpx;
}
.content_list text{
  border:2rpx solid lightgray;
  margin-right:20rpx;
  height: 50rpx !important;
  line-height: 50rpx;
}
.content_list{
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.active{
  border-color:red;
  color:red;
  border: 2rpx solid red !important;
}

没有选择商品规格的效果
在这里插入图片描述
选择商品规格的之后的效果
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值