咨询热线:15811653920

2009-2017

我们一直走在国际设计的前沿,追求与研究从未停止

我的位置:首页 > 学院 > 代码仓库

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

小程序视图
<view class='wraps'>
  <view class='wrap' wx:for="{{options}}" wx:for-index="indexs" wx:for-item="oname">
    <view class='name'>{{oname.name}}</view>
    <view class='des'>
      <view class="{{item.checked?'active':'desitem'}}" wx:for="{{oname.items}}" catchtap="stoggle" data-parentindex="{{indexs}}" data-currentindex="{{index}}">      
        {{item.msg}}      
      </view>      
    </view>
  </view>

</view>


小程序css

.wraps{
  /* width: 100%; */
  padding: 20rpx;
}


.name{
 
  line-height: 100rpx;
}
.des{
  width: 100%;
  
  border: 1px solid #ccc;
  display: flex;
  /* flex-direction: row; */
  /* flex-wrap: wrap; */
}
.desitem{
  display: inline-block;
  background: #ccc;
  padding: 10rpx 20rpx;
  margin-right: 20rpx;
  border-radius: 10rpx;
  
 }
.active{
   background: #f00;
   color: #fff;
 }


小程序业务逻辑:

// pages/text4/text4.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    options: [{
        items: [{
          'msg': '绿色',
          "id": "11",
          "chekced":true
        }, {
          'msg': "红色",
          "id": "12"
        }],
        name: "颜色"
      }, {
        items: [{
          'msg': "XXX",
          "id": "13"
        }, {
          'msg': "L",
          "id": "14"
        }, {
          'msg': "XXL",
          "id": "15"
        }],
        name: "型号"
      }, {
        items: [{
          'msg': "32G",
          "id": "16"
        }, {
          'msg': "8G",
          "id": "17"
        }, {
          'msg': "4G",
          "id": "18"
        }],
        name: "版本"
      }] 
  },

  stoggle: function (e) {
    
    var parentIndex = e.currentTarget.dataset.parentindex;
    var currentIndex = e.currentTarget.dataset.currentindex;
   
   
    var options = this.data.options;

    

    for (var i = 0; i < options.length ;i++){    
   
      if (i == parentIndex){

        console.log(parentIndex);
        for (var j = 0; j < options[i].items.length; j++) {

           options[i].items[j].checked=false;
        }       
        
        options[i].items[currentIndex].checked = true;
      }
    }

    console.log(options);
    this.setData({
      options: options
    })
    
 
   
  } 

 
})

想了解更多,请访问齐创互联,需要网站建设,浏览快速建站模板快速建站
点击在线客服联系客服,联系电话:15811653920 联系人: 丁生.


感谢您关注我们,如果您有建站需求,请与我们取得联系。
扫一扫上面的二维码图案,加我微信
扫一扫上面的二维码图案,加我微信
咨询直线:15811653920 丁生

建站模板

Web Templates

建站龙头,低价保证,贴心服务

权威认证建站龙头企业,合理定价,实现与客户的长期合作,30万家企业级用户优选!