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

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

1、功能实现图示

2、实现思路

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

3、代码实现

vue 复制代码
<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 发布!

相关推荐
咚咚咚小柒5 小时前
【前端】Webpack相关(长期更新)
前端·javascript·webpack·前端框架·node.js·vue·scss
老华带你飞1 天前
房屋租赁|房屋出租|房屋租赁系统|基于Springboot的房屋租赁系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·vue·论文·毕设·房屋租赁系统
前端摸鱼匠1 天前
Vue 3 事件修饰符全解析:从 .stop 到 .passive,彻底掌握前端交互的艺术
前端·vue.js·node.js·vue·交互
Crazy Struggle1 天前
.NET 8.0 + Vue 企业级在线培训系统(开源、免费、支持多种主流数据库)
vue·.net 8.0·后台管理系统
韩立学长2 天前
【开题答辩实录分享】以《植物病虫害在线答疑小程序的设计与实现》为例进行答辩实录分享
spring boot·小程序·vue
whltaoin3 天前
【JAVA全栈项目】弧图图-智能图床 SpringBoot+Vue3 :[框架开荒:一文全步骤打通前后端项目全流程]
java·spring boot·vue·开源项目·全栈·cos
清灵xmf4 天前
Vue + TSX 中使用 class 报错 解决方法
vue
专注前端30年5 天前
Vue2 中 v-if 与 v-show 深度对比及实战指南
开发语言·前端·vue
专注前端30年5 天前
【Vue2】基础知识汇总与实战指南
开发语言·前端·vue
麦麦大数据6 天前
F039 python五种算法美食推荐可视化大数据系统vue+flask前后端分离架构
python·算法·vue·推荐算法·美食·五种算法