el-tree默认展开或折叠,全选或全不选

element-ui中 tree 树形控件的相关功能,演示图:
在这里插入图片描述
代码部分:

<div>
  <el-button type="primary" size="mini" @click="expand">展开/折叠</el-button>
  <el-button type="primary" size="mini" @click="checkAll">全选</el-button>
  <el-button type="primary" size="mini" @click="checkNot">全不选</el-button>
</div>
<el-tree
  :data="list"
  show-checkbox
  node-key="id"
  :props="defaultProps"
  check-on-click-node
  :default-expand-all="isExpand"
  ref="tree"
  @check-change="checkChange">
</el-tree>

js相关:

export default {
  data () {
    return {
      list: [],
      isExpand: true
    }
  },
  methods: {
    expand () { // 展开/折叠
      this.isExpand = !this.isExpand
      const nodes = this.$refs.tree.store._getAllNodes()
      // 或者方法二
      // const nodes = this.$refs.tree.store.nodesMap
      for (let i in nodes) {
        nodes[i].expanded = this.isExpand
      }
    },
    checkAll () { // 全选
      this.$refs.tree.setCheckedNodes(this.list)
      // 或者方法二
      // this.list.forEach(item => {
      //   this.$refs.tree.setCheckedKeys([item.id])
      // })
    },
    checkNot () { // 全不选
      this.$refs.tree.setCheckedKeys([])
    },
}

补充:全选/不全选可以使用一个 checkbox 实现,就不用两个函数啦

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值