elementUI树节点全选,反选,半选状态

javascript 复制代码
// <template>部分
<div class="check-block">
        <el-divider></el-divider>
        <el-checkbox :indeterminate="indeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
        <el-divider></el-divider>
        <el-tree :data="templateList" show-checkbox default-expand-all node-key="id" ref="tree" highlight-current :props="defaultProps" @check="changeCheck">
        </el-tree>
      </div>


data(){
    return {
        indeterminate: false,
        checkAll: false,
        templateList: [],
        treeData: [],
    }
},
methods:{
handleCheckAllChange() {
      if (this.checkAll) {
        for (let i = 0; i < this.templateList.length; i++) {
          if (this.$refs.tree.getNode(this.templateList[i]).disabled == false) {
            this.$refs.tree.setChecked(this.templateList[i].id, true, true);
          }
        }
        this.treeData = this.$refs.tree
          .getCheckedNodes(false, true)
          .map((i) => i.id);
        this.indeterminate = false;
      } else {
        this.$refs.tree.setCheckedNodes([]);
        this.treeData = [];
        this.indeterminate = false;
      }
    },
    changeCheck() {
      this.treeData = this.$refs.tree
        .getCheckedNodes(false, true)
        .map((i) => i.id);
      let arr = [];
      // treeData是所有的节点,templateList是所有父节点,所以还需要遍历treeData取出父节点再将length和templateList做比较
      this.treeData.map((item) => {
        if (this.$refs.tree.getNode(item).isLeaf == false) {
          arr.push(item);
        }
      });
      if (arr.length) {
        if (arr.length == this.templateList.length) {
          this.checkAll = true;
          this.indeterminate = false;
        } else if (arr.length < this.templateList.length) {
          console.log(1111);
          this.checkAll = false;
          this.indeterminate = true;
        } else {
          this.checkAll = false;
          this.indeterminate = false;
        }
      }
    },

}
相关推荐
南玖i3 分钟前
SuperMap iServer + vue3 实现点聚合 超简单!
javascript·vue.js·elementui
web守墓人3 分钟前
【前端】ikun-pptx编辑器前瞻问题四:通过AI编写一个前端pptx编辑器
前端
泰勒疯狂展开5 分钟前
Vue3研学-标签ref属性与TS接口泛型
前端·javascript·vue.js
小二·5 分钟前
前端 DevOps 完全指南:从 Docker 容器化到 GitHub Actions 自动化部署(Vue 3 + Vite)
前端·docker·devops
忒可君5 分钟前
2026新年第一篇:uni-app + AI = 3分钟实现数据大屏
前端·vue.js·uni-app
Komorebi゛6 分钟前
【CSS】线性流动边框样式
前端·css·css3
我不吃饼干14 分钟前
手写 Vue 模板编译(生成篇)
前端·vue.js
s小布丁16 分钟前
vue2纯前端使用Docxtemplater生成word报告,包含echart图表,表格
前端
web小白成长日记9 小时前
企业级 Vue3 + Element Plus 主题定制架构:从“能用”到“好用”的进阶之路
前端·架构
APIshop9 小时前
Python 爬虫获取 item_get_web —— 淘宝商品 SKU、详情图、券后价全流程解析
前端·爬虫·python