el-tree dynamically controls expansion/folding through default-expand-all

1. As shown in the figure below, dynamically control the expansion/collapse, select all/clear through the check box. 2. The
insert image description here
implementation method is as follows: define key, monitor checked2, modify treeKey, re-render tere; add select all and clear.

      <div class="tree">
        <el-checkbox v-model="checked1">选中全部</el-checkbox>
        <el-checkbox v-model="checked2">展开全部</el-checkbox>
        <el-tree :key="treeKey" :data="treeData" show-checkbox node-key="id" :props="defaultProps" ref="tree" :default-expand-all="checked2"></el-tree>
      </div>


    watch: {
    
    
      // 树形控件全选/清空
      'checked1':function (newVale,oldVale) {
    
    
        if(newVale === false) {
    
    
          this.$refs.tree.setCheckedKeys([]);//清空
        } else {
    
    
          this.$refs.tree.setCheckedNodes(this.treeData);//全选
        }
      },
      // 树形控件展开/折叠
      'checked2':function (newVale,oldVale) {
    
    
        this.treeKey = Date.now();//重新渲染树结构,动态控制展开/折叠
      },
    },

Guess you like

Origin blog.csdn.net/DevelopmentW/article/details/132300054