学无先后,达者为师

网站首页 编程语言 正文

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

作者:前端开心果 更新时间: 2023-07-31 编程语言

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 实现,就不用两个函数啦

原文链接:https://blog.csdn.net/qq_38157825/article/details/114657476

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新