el-checkbox 三级联动实现,有图有数据

67 篇文章 2 订阅
22 篇文章 0 订阅

el-checkbox 三级联动实现,一层数据结构和两层数据结构的同步选中取消

效果图

在这里插入图片描述

点击获取数据,即可获取当前选中所有的id

代码

<template>
  <div style="display: flex">
    <div>
      <el-checkbox v-model="AllChecked" @change="allHandle">全选</el-checkbox>
      <div v-for="(item, index) in menuList" :key="item.rId" style="margin-top: 10px">
        二级 {{ index }}
        {{ SecendSeleced[index] }}
        <el-checkbox v-model="SecendSeleced[index]" @change="allHandleSecendSeleced(index, SecendSeleced[index])"
          ><span style="font-weight: bold">{{ item.rName }}</span></el-checkbox
        >
        三级 --{{ index }} ---{{ ThreeSeleced[index] }}
        <div v-for="(item, index) in 3" :key="index">三级触发 --{{ index }} ---{{ ThreeSeleced[index] }}</div>
        <el-checkbox-group style="margin-top: 20px" @change="allHandleThreeSeleced(index, ThreeSeleced[index])" v-model="ThreeSeleced[index]">
          <el-checkbox style="display: inline-block; margin: 5px 0 0 20px" v-for="item in item.resourcesGroupVos" :key="item.rId" :label="item.rId">{{ item.rName }}</el-checkbox>
        </el-checkbox-group>
        111
      </div>
      <el-button @click="getNew()" style="margin: 20px">获取数据</el-button>
    </div>
    <div v-for="(item, index) in n" :key="index">
      <el-checkbox-group v-model="S" @change="aa(S, item.d)">
        <el-checkbox :label="item.rId"></el-checkbox>
      </el-checkbox-group>
      {{ item.rId }}
      <!-- {{ item.d }} -->
    </div>
    <!-- <el-button @click="getIndex()" style="margin: 20px">求索引</el-button> -->
  </div>
</template>
<script>
export default {
  data() {
    return {
      AllChecked: false, // 全选
      list: [
        {
          rName: '子分组1',

          mzy: '1',

          rId: '1',
        },
        {
          rName: '子分组2',

          mzy: '1',

          rId: '11',
        },
        {
          rName: '子分组3',

          mzy: '1',

          rId: '111',
        },
        {
          rName: '子分组1',
          mzy: '2',

          rId: '2',
        },
        {
          rName: '子分组2',
          mzy: '2',

          rId: '22',
        },
        {
          rName: '子分组3',
          mzy: '2',

          rId: '222',
        },
        {
          rName: '子分组1',
          mzy: '3',

          rId: '3',
        },
        {
          rName: '子分组2',
          mzy: '3',

          rId: '33',
        },
        {
          rName: '子分组3',
          mzy: '3',

          rId: '333',
        },
      ],
      menuList: [
        {
          rId: '1',
          rName: '父分组1',
          mzy: '1',

          resourcesGroupVos: [
            { rName: '子分组1', rId: '1' },
            { rName: '子分组2', rId: '11' },
            { rName: '子分组3', rId: '111' },
          ],
        },
        {
          rName: '父分组2',
          rId: '2',
          mzy: '2',

          resourcesGroupVos: [
            { rName: '子分组1', rId: '2' },
            { rName: '子分组2', rId: '22' },
            { rName: '子分组3', rId: '222' },
          ],
        },
        {
          rName: '父分组3',
          rId: '3',
          mzy: '3',

          resourcesGroupVos: [
            { rName: '子分组1', rId: '3' },
            { rName: '子分组2', rId: '33' },
            { rName: '子分组3', rId: '333' },
          ],
        },
      ],
      systemOptions1: [
        {
          rId: '1',
          rName: '父分组1',

          resourcesGroupVos: [
            { rName: '子分组1', rId: '1' },
            { rName: '子分组2', rId: '11' },
            { rName: '子分组3', rId: '111' },
          ],
        },
        {
          rName: '父分组2',
          rId: '2',

          resourcesGroupVos: [
            { rName: '子分组1', rId: '2' },
            { rName: '子分组2', rId: '22' },
            { rName: '子分组3', rId: '222' },
          ],
        },
        {
          rName: '父分组3',
          rId: '3',

          resourcesGroupVos: [
            { rName: '子分组1', rId: '3' },
            { rName: '子分组2', rId: '33' },
            { rName: '子分组3', rId: '333' },
          ],
        },
      ],
      ThreeSeleced: [], // 选中的列表
      SecendSeleced: [], //列表类全选
      systemOptionsList: [], // 所有可选项
      b: [],
      S: [],
      val: [],
      check: [],
      listService: [],
      n: [],
    }
  },
  mounted() {
    this.getData()
    this.getIndex()
  },
  methods: {
    getIndex() {
      for (var i in this.list) {
        var list3 = []
        for (var j in this.list) {
          if (this.list[i].mzy == this.list[j].mzy && this.list[j].key != 1) {
            this.list[j].key = 1

            list3.push(this.list[j])
          }
        }
        if (list3.length > 0) {
          this.listService.push(list3)
        }
      }
      console.log('看你的了this.listService', this.listService)
      this.listService.forEach((val, index) => {
        val.forEach((v, indexv) => {
          v.d = index + '@' + v.rId
        })
      })
      console.log('看你的了this.listService', this.listService)
      this.n = this.listService.reduce((prev, next) => {
        return prev.concat(next)
      })
      console.log('看你的了this.listService', this.n)
    },
    aa(val, index) {
      this.$forceUpdate()
      this.val = val
      console.log('index,val', this.val, index)
      var left = index.split('@')[0]
      var right = index.split('@')[1]
      console.log('left', left, right)

      var index = this.ThreeSeleced[left].indexOf(right)
      console.log('index', index)
      if (index > -1) {
        this.ThreeSeleced[left].splice(index, 1)
      } else {
        this.ThreeSeleced[left].push(right)
      }
      console.log('this.ThreeSeleced2222', this.ThreeSeleced)
      this.SecendSeleced[left] = this.ThreeSeleced[left].length === this.systemOptions1[left].resourcesGroupVos.length
      this.$forceUpdate()

      if (this.val.length === this.list.length) {
        this.AllChecked = true
      } else {
        this.AllChecked = false
      }
    },
    getNew() {
      this.b = []
      for (let i = 0; i < 3; i++) {
        this.b.push(this.ThreeSeleced[i])
      }
      this.check = this.b.reduce((prev, next) => {
        return prev.concat(next) //循环将数组进行拼接
      })
      this.S = this.check
      console.log('统计选中的数据', this.check)
    },
    getData() {
      // 此处应该是接口数据
      for (let item in this.menuList) {
        let strArr = []
        this.ThreeSeleced.push(strArr) // 创建选中数据数组
        console.log('this.ThreeSeleced', this.ThreeSeleced)
        this.systemOptionsList.push(this.menuList[item].resourcesGroupVos) // 创建所有可选项数组
        this.SecendSeleced.push(false) //所有列表类初始为false
      }
    },
    allHandle(val) {
      console.log('val', val)
      // 全选
      let Arrlist = []
      for (let i = 0; i < this.systemOptions1.length; i++) {
        let arr = []
        Arrlist.push(arr)
      }
      for (let index in this.systemOptions1) {
        for (let index1 in this.systemOptions1[index].resourcesGroupVos) {
          Arrlist[index].push(this.systemOptions1[index].resourcesGroupVos[index1].rId)
        }
        this.ThreeSeleced[index] = val ? Arrlist[index] : []
        console.log('全选this.ThreeSeleced', this.ThreeSeleced)
      }
      for (let index in this.SecendSeleced) {
        this.SecendSeleced[index] = val
      }
      this.getNew()
    },
    allHandleSecendSeleced(index, val) {
      // 列表类全选
      let arr = []
      for (let item in this.systemOptionsList[index]) {
        arr.push(this.systemOptionsList[index][item].rId)
      }
      this.ThreeSeleced[index] = val ? arr : []
      this.getAllState()
    },
    allHandleThreeSeleced(index, value) {
      console.log('index, value', index, value)
      // 子选项联动
      this.$forceUpdate() // !!!实时改变数据
      let b = value.length === this.systemOptionsList[index].length
      this.SecendSeleced[index] = b
      this.getAllState()
    },
    getAllState() {
      // 判断列表类是否全选来改变总的全选状态
      let [...arrFlag] = new Set(this.SecendSeleced)
      console.log('arrFlag', arrFlag)
      if (arrFlag.length === 1 && arrFlag[0] === true) {
        this.AllChecked = true
      } else {
        this.AllChecked = false
      }
      console.log('AllChecked', this.AllChecked)
      this.getNew()
    },
  },
}
</script>

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现三级联动el-checkbox-group,可以采用以下几个步骤: 1. 定义三个数组分别代表三级数据,例如: ``` data() { return { level1: [ { label: 'A', value: 'a' }, { label: 'B', value: 'b' }, { label: 'C', value: 'c' }, ], level2: { a: [ { label: 'A1', value: 'a1' }, { label: 'A2', value: 'a2' }, ], b: [ { label: 'B1', value: 'b1' }, { label: 'B2', value: 'b2' }, ], c: [ { label: 'C1', value: 'c1' }, { label: 'C2', value: 'c2' }, ], }, level3: { a1: [ { label: 'A1-1', value: 'a1-1' }, { label: 'A1-2', value: 'a1-2' }, ], a2: [ { label: 'A2-1', value: 'a2-1' }, { label: 'A2-2', value: 'a2-2' }, ], b1: [ { label: 'B1-1', value: 'b1-1' }, { label: 'B1-2', value: 'b1-2' }, ], b2: [ { label: 'B2-1', value: 'b2-1' }, { label: 'B2-2', value: 'b2-2' }, ], c1: [ { label: 'C1-1', value: 'c1-1' }, { label: 'C1-2', value: 'c1-2' }, ], c2: [ { label: 'C2-1', value: 'c2-1' }, { label: 'C2-2', value: 'c2-2' }, ], }, selected: [], }; }, ``` 2. 在模板中使用三个 el-checkbox-group 分别渲染三级数据,例如: ``` <el-checkbox-group v-model="selected"> <div v-for="item in level1" :key="item.label"> <label>{{ item.label }}</label> <el-checkbox :label="item.value" @change="onLevel1Change(item.value)"></el-checkbox> </div> <div v-for="item in level2[selected[0]]" :key="item.label"> <label>{{ item.label }}</label> <el-checkbox :label="item.value" @change="onLevel2Change(item.value)"></el-checkbox> </div> <div v-for="item in level3[selected[1]]" :key="item.label"> <label>{{ item.label }}</label> <el-checkbox :label="item.value"></el-checkbox> </div> </el-checkbox-group> ``` 3. 在组件中定义三个方法,用于处理三级数据的选中状态,例如: ``` methods: { onLevel1Change(value) { this.selected.splice(0, 1, value); this.selected.splice(1, 1); }, onLevel2Change(value) { this.selected.splice(1, 1, value); }, }, ``` 这样就可以实现三级联动el-checkbox-group 了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值