移动端sku

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  <style>
    .spu-selectbox {
      padding: 10px;
      color: #333;
    }

    .listbox {
      padding: 10px 0;
    }

    .commodity {
      padding-left: 15px;
      margin-top: 5px;
    }

    .content {
      display: inline-block;
      padding: 3px 5px;
      margin: 0 5px;
      box-sizing: border-box;
      background: #ebebeb;
      border-radius: 8px;
      border: 1px solid transparent;
    }

    .active {
      border: 1px solid rgb(194, 63, 63);
    }

    .disable {
      background: #f3f3f3;
      color: #999;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="spu-selectbox">
      <!-- 这个是标题想加就可以加 -->
      <!-- <p>{{simulatedDATA.title}}</p> -->
      <div>
        规格型号:<a v-for="(item,index) in select" :key="index"> {{item.label}}</a>
      </div>
      <div v-for="(item,index) in typelist" :key="index" class="listbox">
        <div>{{item.name}}:</div>
        <div class="commodity">
          <a v-for="(items,indexs) in item.item" :key="indexs"
            :class="['content',{'active':items.active},{'disable':items.disabled}]"
            @click="selectTag(index,indexs,items)">{{items.label}}</a>
        </div>
      </div>
    </div>
  </div>

  <script>
    let simulatedDATA = {
      title: '手机',
      difference: [
        {
          id: '1',
          price: '200.00',
          stock: '59',
          difference: '128G,100,白色'
        },
        {
          id: '2',
          price: '200.00',
          stock: '19',
          difference: '128G,100,蓝色'
        },
        {
          id: '3',
          price: '200.00',
          stock: '0',
          difference: '128G,100,黑色'
        },
        {
          id: '4',
          price: '259.00',
          stock: '0',
          difference: '128G,200,白色'
        },
        {
          id: '5',
          price: '200.00',
          stock: '19',
          difference: '128G,200,蓝色'
        },
        {
          id: '6',
          price: '200.00',
          stock: '19',
          difference: '128G,200,黑色'
        },
        {
          id: '7',
          price: '259.00',
          stock: '0',
          difference: '256G,100,白色'
        },
        {
          id: '8',
          price: '590.00',
          stock: '1',
          difference: '256G,100,蓝色'
        },
        {
          id: '9',
          price: '200.00',
          stock: '0',
          difference: '256G,100,黑色'
        },
        {
          id: '10',
          price: '359.00',
          stock: '0',
          difference: '256G,200,白色'
        },
        {
          id: '11',
          price: '300.00',
          stock: '0',
          difference: '256G,200,蓝色'
        },
        {
          id: '12',
          price: '399.00',
          stock: '0',
          difference: '256G,200,黑色'
        },
        {
          id: '13',
          price: '399.00',
          stock: '0',
          difference: '300G,200,黑色'
        }
      ],
      //可选择商品
      specifications: [
        {
          //这里是要被渲染字段
          name: '型号',
          item: [
            {
              label: '128G'
            },
            {
              label: '256G'
            },
            {
              label: '300G'
            }
          ]
        },
        {
          name: '尺寸',
          item: [
            {
              label: '100'
            },
            {
              label: '200'
            }
          ]
        },
        {
          name: '颜色',
          item: [
            {
              label: '白色'
            },
            {
              label: '蓝色'
            },
            {
              label: '黑色'
            }
          ]
        }
      ]
    }
    new Vue({
      el: '#app',
      data() {
        return {
          typelist: [], //渲染列表
          SkuList: [], //库存列表
          select: [] //已选列表
        }
      },
      mounted() {
        this.skulist() //库存
        this.get_skulist() //渲染列表
        this.init()//进入页面先把没有库存的置灰
      },
      methods: {
        init() {
          console.log(this.SkuList.flat(Infinity),'库存列表降维');
          this.typelist.forEach(a => {
            a.item.forEach(b => {
              if (!this.SkuList.flat(Infinity).includes(b.label)) {
                b.disabled = true
              } else { 
                b.disabled = false
              } 
            })
          })
        },
        get_skulist() {
          // 提出渲染列表
          simulatedDATA.specifications.forEach(res => {
            res.item.forEach(i => {
              i.disabled = false
              i.active = false
            })
          })
          this.typelist = simulatedDATA.specifications
          console.log(this.typelist,'渲染列表');
        },
        skulist() {
          // 库存列表
          let arr = simulatedDATA.difference
          let pro_all_in = []
          arr.forEach(item => {
            if (item.stock != '0') {
              pro_all_in.push(item['difference'].split(','))
            }
          })
          this.SkuList = pro_all_in
          console.log(this.SkuList,'库存列表');
        },
        //点击事件
        selectTag(index, tagindex, item) {
          //index-大数组索引,tagindex-小数组索引,item-小数组数据
          if (item.disabled == true) return //不可选直接跳过
          let select = this.select //已选列表
          let typelist = this.typelist //渲染列表
          //   let SkuList = this.SkuList //库存列表
          if (item.active) {
            typelist[index]['item'][tagindex].active = false
            select.forEach((a, i) => {
              if (a.index == index) {
                select.splice(i, 1)
              }
            })
            console.log(select,'选中色移除');
          } else {
            typelist[index]['item'].forEach(a => {
              a.active = false
            })
            typelist[index]['item'][tagindex].active = true
            select.forEach((a, i) => {
              if (a.index == index) {
                select.splice(i, 1)
              }
            })
            select.push({ label: item.label, index, tagindex })
          }
          //大数组索引升序
          select.sort(function (a, b) {
            return a.index - b.index
          })
          let x_selected = []
          select.forEach(item => {
            x_selected.push(item.index)
          })
          if (select.length == 0) {  //没有选中
            //没选就重新渲染下
            this.init()
          } else {   //有选中
            typelist.forEach((el, idx) => {
              el.item.forEach((pro, index) => {
                if (!pro.active) {
                  pro.disabled = true
                }
                let data = {
                  label: pro.label,
                  index: idx,
                  tagindex: index
                }
                if (x_selected.indexOf(idx) > -1) {
                  let sel = select.slice()
                  let c = x_selected.indexOf(idx)
                  sel.splice(c, 1, data)
                  this.optionsHandle(sel, data)
                } else {
                  let sel = select.slice()
                  sel.push(data)
                  this.optionsHandle(sel, data)
                }
              })
            })
            console.log(select,'最后选中的数组项');
          }
        },
        optionsHandle(sel, data) {
          let arr = []
          sel.map(item => {
            arr.push(item.label)
          })
          this.SkuList.map(i => {
            if (this.isContained(i, arr)) {
              this.typelist[data.index]['item'][data.tagindex].disabled = false
            }
          })
          
        },
        isContained(aa, bb) {
          // 全部的 ,  选中的
          //判断aa数组是否 全 包含bb数组
          //   console.log(aa,bb)
          if (
            !(aa instanceof Array) ||
            !(bb instanceof Array) ||
            aa.length < bb.length
          ) {
            return false
          }
          let aaStr = aa.toString()
          for (var i = 0; i < bb.length; i++) {
            // console.log(aaStr, '-----', bb[i], aaStr.indexOf(bb[i]) < 0)
            if (aaStr.indexOf(bb[i]) < 0) {
              //小于 0 没有
              return false
            }
          }
          return true
        }
      }
    })

  </script>

</body>

</html>
```备注:代码借鉴别人,仅用作记录学习,如有侵权,请联系删除

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值