小程序 商品规格选择

goods.js

//获取应用实例
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    gid: 1,
    gindex: 0,
    changegoodsSpec: '',//默认
    goodsSpec: [
      { id: 1, specName: '规格一', repertory:0},
      { id: 2, specName: '规格二', repertory:1},
      { id: 3, specName: '规格三', repertory:5}
      ], //商品规格

  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 规格选择
    guige (e) {
      var changegoodsSpec = this.data.goodsSpec[e.currentTarget.dataset.current].specName;
      var spacIndex = e.currentTarget.dataset.current;
      if (this.data.goodsSpec[spacIndex].repertory != 0) {//规格数量大于0可选
        this.setData({
          gid: e.currentTarget.dataset.index,
          gindex: e.currentTarget.dataset.current,
          changegoodsSpec: changegoodsSpec,
        })
      }
      console.log("gggg规格:" + spacIndex + "/index:" + this.data.gid)
      // var type = this.data.type;
     
      var specName = this.data.goodsSpec[spacIndex].specName;
      var spacId = this.data.goodsSpec[spacIndex].id;
      var repertory = this.data.goodsSpec[spacIndex].repertory; //库存

      if (this.data.goodsSpec[spacIndex].repertory != 0) {//规格数量大于0可选
        this.setData({
          specName: specName,
          spacId: spacId,//规格id
          repertory: repertory,//库存
        })
      }
      if (repertory == 0) {
        wx.showToast({
          title: '当前规格无库存',
          icon: 'none',
          duration: 2000
        })
      }
    },
  }
})

goods.wxml

<view class="container">
  <!-- 商品规格 -->
    <view class='setMeal_head'>选择规格</view>
      <view class='setMeal'>
        <block wx:for="{{goodsSpec}}">
          <!-- <view class='content_title'>{{item.name}}</view> -->
          <view id="{{item.repertory==0?'soldOut':''}}" class="content_title {{gindex==index?'selected':''}}"  bindtap='guige' data-index='{{item.id}}' data-current='{{index}}'>{{item.specName}}</view>

        </block>
    </view>
  <!-- 商品规格end -->
</view>

goods.wxss

.setMeal{
  height: 135px;
}
.setMeal_head {
  font-size: 16px; 
  color: #414141;
  line-height: 35px;
  font-weight: bold;
}
.content_title {
  height: 35px;
  line-height: 35px;
  padding: 0px 8px;
  display: inline-block;
  margin-right: 10px;
  background: #f2f2f2;
  color: #414141;
  margin-top: 10px;
  font-size: 14px;
}
.nowselect {
  color: #414141;
}

.selected {
  background: #fdeed9;
  color: #ff9500;
}

.selectkuai {
  border: solid 1px black;
  margin: 20rpx;
  padding: 5rpx 20rpx;
  border-radius: 20rpx;
}
#soldOut{
 background-image: repeating-linear-gradient(-45deg,rgba(255, 255, 255,1),rgba(255, 255, 255,1) 8px,rgba(242, 242, 242, 1) 0,rgba(242, 242, 242, 1) 13px);
  color: #8E8E93;
}

效果图
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值