el-tree设置了默认展开节点属性,在对树记录展开关闭操作时会出现关闭不了节点的问题

问题描述:为了每次更改后看起来更加友好,保存上次操作完项来默认展开,因此添加了default-expanded-keys属性。添加之后发现,默认展开的项通过控制node 无法实现关闭,只能点前面的icon 来关闭。

思路:在网上看别人使用auto-expand-parent来控制是否默认打开,发现并不能实现。后面发现在接口调用后手动清除default-expanded-keys可以恢复。

解决方法:default-expanded-keys只在调取接口时设置,在调取完之后手动清除

代码:

getData(isOpenAtricle = false) {
      this.directoryList().then(res => {
        this.handleData(res)
        this.treeList = res
        //此处从点击过的节点中获取默认展开
        if (!_.isEmpty(this.currentTree)) this.defaultActiveId = this.currentTree.id
        this.$nextTick(() => {
          //设置选中状态并清空默认展开的值
          this.$refs.helpTree.setCurrentNode(this.defaultActiveId)
          this.defaultActiveId = ''
        })
      })
 },
 // 用于处理树形数据
    handleData(data, level = 1) {
      // 遍历数组
      for (let item of data) {
        item.level = level
        !item.title && (item.title = item.directoryName)
        if (Array.isArray(item.childDirectories) && item.childDirectories.length > 0) {
          item.children = item.childDirectories // 重命名键名
          delete item.childDirectories // 删除旧键名
          this.handleData(item.children, 2)
        }
        if (Array.isArray(item.articles) && item.articles.length > 0) {
          item.children = item.articles // 重命名键名
          delete item.articles // 删除旧键名
          item.children.forEach(article => {
            article.level = 3
            // 初始时设置默认的节点
            if (_.isEmpty(this.currentTree)) this.currentTree = article
          })
        }
      }
    },

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值