日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

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 實現(xiàn),就不用兩個函數(shù)啦

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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新