vue2商品sku

vue2商品sku

第一步针对于没有任何可以判断是否选中的就在数据中增加一个自己觉得辨识可以的字段存放下表默认如果默认有选中项就把-1改成对应的,包括计数器可以在初始化就定义一个转发或者提交就添加进去强制更新data

<template>
  <el-dialog :before-close="handleClose" :show-close="false" title="选择规格" :visible.sync="dialogVisible" width="60%">
    <span>
      <div v-for="(ym, index) in qustion" :key="index" class="questions">
        <div class="ym_checkname">{{ ym.value }}</div>
        <div class="ym_select">
          <div v-for="(kys, ind) in ym.leaf" :key="ind" class="ym_keys" :class="{ active: ym.itemIndex == ind }" @click="clik(index, ind)">{{ kys.value }}</div>
        </div>
      </div>
      <div class="ym_checkname">库存</div>
      <div class="ym_stock">
        {{ stock }}
      </div>
      <div class="ym_checkname">数量</div>
      <el-input-number v-model="quantit" label="描述文字" :min="1" />
    </span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="clear">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { choosTree } from '@/api/mall/goodspu/goodspu'
  export default {
    props: {
      showtab: {
        type: Boolean,
        default: () => {},
      },
      spuid: {
        type: Number,
        default: () => {},
      },
    },
    data() {
      return {
        dialogVisible: this.showtab,
        spu: this.spuid,
        userlist: {},
        stock: '',
        quantit: 1, //购买数量
        qustion: [],
        form: {},
      }
    },
    watch: {
      spuid(newValue, oldValue) {
        if (newValue !== oldValue) {
          this.spu = newValue
          this.qustion = []
          this.form = {}
        }
      },
      showtab(newValue, oldValue) {
        if (newValue != oldValue) {
          this.dialogVisible = newValue
          this.quantit = 1
          this.qustion = []
          this.choosTree()
        }
      },
    },
    mounted() {
      this.choosTree()
    },
    methods: {
      handleClose() {},
      choosTree() {
        choosTree({ spuId: this.spu }).then((res) => {
          this.userlist = res.data
          if (res.data.specs.length < 1) {
            this.form = res.data.skus[0]
          } else {
            this.userlist.specs.forEach((element) => {
              Object.assign(element, { itemIndex: -1 })
              if (element) {
                this.qustion.push(element)
              }
            })
          }
          this.stock = this.userlist.skus[0].stock
        })
      },
      clik(e, ind) {
        this.stock = ''
        if (this.qustion[e].itemIndex === ind) {
          this.qustion[e].itemIndex = -1
        } else {
          this.qustion[e].itemIndex = ind
        }
        let array = []
        this.qustion.forEach((ruslt) => {
          if (ruslt.itemIndex > -1) {
            array.push(ruslt.leaf[ruslt.itemIndex].id)
            this.userlist.skus.forEach((row) => {
              if (array.join(',') === row.specsId) {
                this.stock = row.stock
                this.form = {}
                this.form = row
              }
            })
          }
        })
        this.stock = this.userlist.skus[ind].stock
      },
      clear() {
        this.$emit('selsku', null)
        this.qustion = []
      },
      save() {
        if (this.userlist.specs.length > 0) {
          let bolean = this.qustion.every((item) => item.itemIndex > -1)
          if (bolean) {
            this.qustion = []
            console.log(this.quantit)
            this.$set(this.form, 'quantity', this.quantit)
            this.$emit('selsku', this.form)
          } else {
            this.$message.error('请补充规格信息')
          }
        } else {
          this.qustion = []
          console.log(this.quantit)
          this.$set(this.form, 'quantity', this.quantit)
          this.$emit('selsku', this.form)
        }
      },
    },
  }
</script>

<style lang="scss" scoped>
  .ym_checkname {
    font-weight: bold;
    line-height: 40px;
    color-interpolation-filters: #333;
  }
  .ym_select {
    margin: 10px 0;
    .ym_keys {
      display: inline-block;
      padding: 8px;
      margin: 0 5px;
      cursor: pointer;
      border: 1px solid #e8e8e8;
      border-radius: 5px;
      &:hover {
        border: 1px solid #3782ff;
      }
    }
    .active {
      border: 1px solid #3782ff;
    }
  }
  ::v-deep .el-dialog__header {
    border-bottom: 1px dashed #e8e8e8;
  }
</style>

感兴趣就拿去玩玩
本人菜鸡大佬留情

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进阶的小趴菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值