element-ui tree树形结构全选、取消全选,展开收起

控制树形结构全选、取消全选,展开收起

复制代码
<template>
	<div>
		<!-- 添加 ref="tree" 属性-->
		<el-tree
		  :data="data"
		  show-checkbox
		  default-expand-all
		  node-key="id"
		  ref="tree"
		  highlight-current
		  :props="defaultProps">
		</el-tree>
		
		<div class="buttons">
		  <el-button @click="allExpanded">折叠/展开</el-button>
		  <el-button @click="allChecked">全选</el-button>
		  <el-button @click="resetChecked">清空</el-button>
		</div>
	</div>
</template>
 
<script>
  export default {
    methods: {
		 // this.$refs.tree  tree是上面控件 el-tree 的 ref 属性的值
	  allExpanded(){
		  // 展开还是收起
		  this.expanded = !this.expanded;
		  var treeNodeList = this.$refs.tree.store._getAllNodes();
		  for (var i = 0; i < treeNodeList.length; i++) {
		  	treeNodeList[i].expanded = this.expanded;
		  }
	  },
      allChecked() {
		  // 将根节点赋值上去
        this.$refs.tree.setCheckedKeys([1,2,3]);
      },
      resetChecked() {
		  // 将选中设置为空
        this.$refs.tree.setCheckedKeys([]);
      }
    },
 
    data() {
      return {
		expanded:true,
        data: [{
          id: 1,
          label: '一级 1',
          children: [{
            id: 4,
            label: '二级 1-1',
            children: [{
              id: 9,
              label: '三级 1-1-1'
            }, {
              id: 10,
              label: '三级 1-1-2'
            }]
          }]
        }, {
          id: 2,
          label: '一级 2',
          children: [{
            id: 5,
            label: '二级 2-1'
          }, {
            id: 6,
            label: '二级 2-2'
          }]
        }, {
          id: 3,
          label: '一级 3',
          children: [{
            id: 7,
            label: '二级 3-1'
          }, {
            id: 8,
            label: '二级 3-2'
          }]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    }
  };
</script>
相关推荐
问心无愧05138 小时前
ctf show web入门160 161
前端·笔记
李小白668 小时前
第四天-WEB服务器基本原理,IIS服务
运维·服务器·前端
humcomm8 小时前
AI编程时代新前端职位
前端·ai编程
好家伙VCC9 小时前
Web Components主题热切换方案揭秘
java·前端
甲维斯9 小时前
Kimi版超级玛丽效果“惊人”,配额不足5厘米!
前端·人工智能
hboot9 小时前
AI工程师第一课 - Python
前端·后端·python
凉菜凉凉9 小时前
AI时代,被抛弃的前端
前端·ai
console.log('npc')10 小时前
AI前端工程与生成式UI学习路线
前端·人工智能·ui
huangdong_10 小时前
淘宝商品SKU图自动分类技术深度解析:从DOM解析到智能归档
开发语言·javascript·ecmascript
梦曦i10 小时前
uni-router v1.1.1发布:守卫超时保护+路由监听
前端·uni-app