Tree全部展开/折叠

<template>

  <el-tree :data="navs" :props="defaultProps" show-checkbox ref="tree" highlight-current draggable  node-key="navId"   :default-expand-all="defaultExpand"> </el-tree>

  <el-button @click="setCheckedNodes">展开所有菜单</el-button>

  <el-button @click="setCheckedKeys">收起所有菜单</el-button>

</template>

<script>

export default { 
  data() {
    return {

      navs:[],   //所有菜单 

        defaultProps: {
        id:'navId',
        children: 'snavList',
        label: 'navName',
        disabled:'navOffOn'
        },

      defaultExpand:true,  //默认展开所有节点

    }

  },

  methods: {

    setCheckedKeys() {  
      this.defaultExpand=false;
      for(var i=0;i<this.$refs.tree.store._getAllNodes().length;i++){
        this.$refs.tree.store._getAllNodes()[i].expanded=this.defaultExpand;
      }
    },

    setCheckedNodes() {
      this.defaultExpand=true; //展开所有节点
      for(var i=0;i<this.$refs.tree.store._getAllNodes().length;i++){
        this.$refs.tree.store._getAllNodes()[i].expanded=this.defaultExpand;
      }
    }

  }

}

</script>

猜你喜欢

转载自www.cnblogs.com/miaoyiyan/p/9486210.html