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;
        }
      }
    },

}
相关推荐
CodeSheep4 分钟前
JetBrains官宣,又一个IDE可以免费用了!
前端·后端·程序员
刘新明19896 分钟前
Frida辅助分析OLLVM虚假控制流程(下)
java·开发语言·前端
江城开朗的豌豆23 分钟前
小程序登录不迷路:一篇文章搞定用户身份验证
前端·javascript·微信小程序
aesthetician27 分钟前
React 19.2.0: 新特性与优化深度解析
前端·javascript·react.js
FIN666842 分钟前
射频技术领域的领航者,昂瑞微IPO即将上会审议
前端·人工智能·前端框架·信息与通信
U.2 SSD1 小时前
ECharts漏斗图示例
前端·javascript·echarts
江城开朗的豌豆1 小时前
我的小程序登录优化记:从短信验证到“一键获取”手机号
前端·javascript·微信小程序
excel1 小时前
Vue Mixin 全解析:概念、使用与源码
前端·javascript·vue.js
IT_陈寒1 小时前
Java性能优化:这5个Spring Boot隐藏技巧让你的应用提速40%
前端·人工智能·后端
勇往直前plus1 小时前
CentOS 7 环境下 RabbitMQ 的部署与 Web 管理界面基本使用指南
前端·docker·centos·rabbitmq