联动选择初级

本文描述了如何在Vue应用中使用V-for指令创建动态选项列表,用户点击按钮时更新选项状态,并基于用户选择同步数据。通过map和filter方法实现选项的去重和状态管理。
摘要由CSDN通过智能技术生成
<template>
    <div>
        <div class="test">
          <button v-for="(item, index) in allOptions.options"  
          :key="index" :class="['option', item.isCheckout ? 'checked' : '']" 
          @click="handleSome(item, 'options', 'one')">
            {{ item.name }}
          </button>
        </div>
        <div class="test">
          <button v-for="(item, index) in allOptions.options2"  
          :key="index" :class="['option', item.isCheckout ? 'checked' : '']" 
          @click="handleSome(item, 'options2', 'two')">
            {{ item.name }}
          </button>
        </div>
        <div class="test">
          <button v-for="(item, index) in allOptions.options3"  
          :key="index" :class="['option', item.isCheckout ? 'checked' : '']" 
          @click="handleSome(item, 'options3', 'three')">
            {{ item.name }}
          </button>
        </div>
    </div>
</template>

<script>
 data(){
    return {
       allOptions: {
        options: [],
        options2: [],
        options3: [],
      },
      tableList10086: []
        }
    },

methods: {
    testData() {
      const tableList = [
        {id: 52, good_id: 49, one: "金属", two: "白色", three: "18cm", inventory: "1", price: "1.00"},
        {id: 53, good_id: 49, one: "塑料", two: "白色", three: "12cm", inventory: "2", price: "2.00"},
        {id: 54, good_id: 49, one: "金属", two: "黑色", three: "18cm", inventory: "3", price: "3.00"},
        {id: 55, good_id: 49, one: "塑料", two: "黑色", three: "15cm", inventory: "4", price: "4.00",},
        {id: 56, good_id: 49, one: "铝", two: "紫色", three: "15cm", inventory: "5", price: "5.00"}
      ]
      
      const tableList1 = [
        {id: 52, good_id: 49, one: "", two: "白色", three: "18cm", inventory: "1", price: "1.00"},
        {id: 53, good_id: 49, one: "", two: "白色", three: "12cm", inventory: "2", price: "2.00"},
        {id: 54, good_id: 49, one: "", two: "黑色", three: "18cm", inventory: "3", price: "3.00"},
        {id: 55, good_id: 49, one: "", two: "黑色", three: "15cm", inventory: "4", price: "4.00",},
        {id: 56, good_id: 49, one: "", two: "紫色", three: "15cm", inventory: "5", price: "5.00"}
      ]

      this.tableList10086 = tableList1
      let options =this.tableList10086.map(item => {
        return {
          name: item.one,
          isCheckout: false
        }
      })
      let options2 = this.tableList10086.map(item => {
        return {
          name: item.two,
          isCheckout: false
        }
      })
      let options3 = this.tableList10086.map(item => {
        return {
          name: item.three,
          isCheckout: false
        }
      })

      /**
       * map 对象去重
       * @param {Array} arr 要去重的数组
       * @returns {Array} 去重后的数组
       * @example
       * const arr = [{name: 'a'}, {name: 'b'}, {name: 'a'}]
       * let map = new Map()
       * for(let item of arr) {
       *    map.set(item.name, item) // 参数一表示要去重的值,参数二表示去重后的值
       * }
       * const res = [...map.values()] // 去重之后的数组
       * console.log(res)
       */
      let map = new Map()
      for(let item of options) {
        map.set(item.name, item)
      }
      options = [...map.values()].filter(item => item.name !== '')

      map = new Map()
      for(let item of options2) {
        map.set(item.name, item)
      }
      options2 = [...map.values()].filter(item => item.name !== '')

      map = new Map()

      for(let item of options3) {
        map.set(item.name, item)
      }
      options3 = [...map.values()].filter(item => item.name !== '')

      this.allOptions = {
        options,
        options2,
        options3
      }
    },

    handleSome(item, type, key) {
      this.allOptions[type] = this.allOptions[type].map(i => {
        if(i.name === item.name) {
          i.isCheckout = true
        } else {
          i.isCheckout = false
        }
        return i
      })

      // 对应的数据
      const newD = this.tableList10086.filter(i => i[key] === item.name)
      const type1 = {
        'options': 'one',
        'options2': 'two',
        'options3': 'three'
      }
      Object.keys(this.allOptions).forEach(K => {
        // 不要自己和自己对比
        if(K !== type) {
          this.allOptions[K] = this.allOptions[K].map(i => {
            return {
              name: i.name,
              isCheckout: newD.some(item => item[type1[K]] === i.name)
            }
          })
        }
      })
      console.log(newD);
    }

}
</script>

<style>

  .test {
    display: flex;
    .option {
      margin: 10px;
    }

    .checked {
      background-color: pink;
    }
  }
</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值