当前位置: 首页 > article >正文

el-tree父子不互相关联时,手动实现全选、反选、子级全选、清空功能

el-tree父子不互相关联时,手动实现全选、反选、子级全选、清空功能

1、功能实现图示

在这里插入图片描述

2、实现思路

当属性check-strictly为true时,父子节点不互相关联,如果需要全部选中或选择某一节点下的全部节点就必须手动选择每个节点,十分麻烦。可以通过ref操做el-tree的getCheckedKeys、getCheckedNodes、setCheckedKeys方法手动快速节点选择。

3、代码实现

<template>
  <div class="list_tree">
    <div class="flex mb10">
      <el-button
        v-for="item in treeButtonProps"
        size="mini"
        type="primary"
        class="mr5"
        :key="item.treeKey"
        :disabled="item.isDisb ? isdisChildAll : false"
        @click="onChecked(item.treeKey)"
      >
        {{ item.text }}
      </el-button>
    </div>
    <el-tree
      ref="treeRef"
      :data="treeData"
      show-checkbox
      node-key="deptId"
      check-strictly
      default-expand-all
      @check-change="checkChange"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      // tree数据结构....
      treeData: [
        {
          deptId: '130200',
          label: '河北省/唐山市',
          pid: null,
          regionCode: '130200',
          type: '1',
          topId: null,
          children: [
            {
              deptId: '13020001',
              label: '唐山教育局',
              pid: '130200',
              regionCode: '130200',
              type: '2',
              topId: '130200',
              children: [
                {
                  deptId: '130200001',
                  label: '唐山初级中学校',
                  pid: '13020001',
                  regionCode: '130200',
                  type: '2',
                  children: null,
                  topId: '130200'
                },
                {
                  deptId: '130200002',
                  label: '唐山市初级二中',
                  pid: '13020001',
                  regionCode: '130200',
                  type: '2',
                  children: null,
                  topId: '130200'
                }
              ]
            }
          ]
        }
        /// more-data.......
      ],
      isdisChildAll: false,
      treeKeysList: [],
      treeButtonProps: [
        { text: '全选', isDisb: false, treeKey: 'all' },
        { text: '反选', isDisb: false, treeKey: 'reverse' },
        { text: '子级全选', isDisb: true, treeKey: 'childAll' },
        { text: '清空', isDisb: false, treeKey: 'clear' }
      ]
    };
  },
  methods: {
    // 获取树所有key集合
    getTreeKeys() {
      this.treeKeysList = [];
      const treeData = deepClone(this.treeData);
      while (treeData.length > 0) {
        const item = treeData.pop();
        this.treeKeysList.push(item.deptId);
        if (item.children && item.children.length > 0) {
          treeData.push(...item.children);
        }
      }
    },
    // 设置子级全选是否禁用
    checkChange(data, checked) {
      // 没有选中含有子级节点时禁用
      if (checked) {
        this.isdisChildAll = !(data.children && data.children.length > 0);
      } else {
        this.isdisChildAll = true;
      }
    },
    // 全选、反选、子级全选、清空
    onChecked(type) {
      // 最终选中的keys
      let setKeysList = [];
      const treeNode = this.$refs.treeRef;
      // 已选中keys
      const checkedKeys = treeNode.getCheckedKeys();
      if (type == 'clear') {
        setKeysList = [];
      }
      if (type == 'all') {
        setKeysList = this.treeKeysList;
      }
      if (type == 'reverse') {
        // 未选中keys集合
        setKeysList = this.treeKeysList.filter(item => checkedKeys.indexOf(item) == -1);
      }
      if (type == 'childAll') {
        setKeysList = checkedKeys;
        // 目前被选中的节点所组成的数组
        const checkNodes = treeNode.getCheckedNodes();
        // 筛选出有子节点的node
        const hasChildNodes = checkNodes.filter(item => item.children && item.children.length > 0);
        // 循环遍历出子节点集合
        while (hasChildNodes.length > 0) {
          const item = hasChildNodes.pop();
          setKeysList.push(item.deptId);
          if (item.children && item.children.length > 0) {
            hasChildNodes.push(...item.children);
          }
        }
      }
      // 设置节点选中状态
      treeNode.setCheckedKeys(setKeysList);
    }
  }
};
</script>

本文由博客一文多发平台 OpenWrite 发布!


http://www.kler.cn/news/304391.html

相关文章:

  • Qt 构建报错 undefined reference to xxx
  • C++字符串中的string类操作
  • Humanize AI 简介
  • C和指针:函数
  • Leetcode 701-二叉搜索树中的插入操作
  • 安卓开发板_联发科MTK开发板使用ADB开发
  • Excel--不规则隔行填充底纹颜色
  • 【动手学深度学习】08 线性回归 + 基础优化算法(个人向笔记)
  • SpringCloud神领物流学习笔记:项目概述(一)
  • 计算机网络 ---如何寻找目标计算机
  • C语言——双指针法求有序数组的平方
  • Linux:五种IO模型
  • 学习笔记 韩顺平 零基础30天学会Java(2024.9.13)
  • 智能听诊器:打造宠物个性化健康生活
  • 数学基础 -- 概率统计之高斯分布
  • 后端开发刷题 | 把数字翻译成字符串(动态规划)
  • Linux sh命令
  • 【Linux】深刻理解操作系统的管理
  • 若依plus- cloud RuoYiGatewayApplication :8080/(ruoyi-gateway)启动不了,报错!
  • 鸿蒙 - 判断手机号、身份证(正则表达式)
  • CMake构建学习笔记16-使用VS进行CMake项目的开发
  • 计算机组成原理(第二次笔记)
  • PHP高效协同无缝对接一站式生产管理系统小程序源码
  • 深入理解指针(二)
  • vue3里根据配置信息显示el-button的问题
  • iOS中的链表 - 单向链表
  • 多核DSP(6000系列)设计与调试技巧培训
  • 【案例70】invalid secrity token(null)
  • 【SpringBoot】调度和执行定时任务--DelayQueue (附demo)
  • STM32——看门狗通俗解析