【elementui】记录el-tree默认选中某节点并高亮显示

elementui: V2.8.2
要求:默认选中第一个叶结点

高亮显示::highlight-current="true"

<el-tree
   class="tree"
   ref="tree"
   node-key="key"
   :data="treeData"
   :props="{  
     children: 'children',  
     label: 'label',
     id: 'id',
     key: 'key',  
   }"
   :highlight-current="true"
   :expand-on-click-node="false"
   :default-expand-all="true"
   @node-click="handleNodeDblclick"  
>

这是一段递归el-tree数据的js,因为treeData可能有很多层不确定,也可能第一个节点里面没有子结点但它并不是我认定的叶结点,我的叶结点的标识是:istag: true

export const findFirstTagNode = (nodes) => {
  for(let node of nodes) {
    if(node.children && node.children.length>0) {
      for(let child of node.children) {
        if(child.istag) {
          return child
        }

        let found = findFirstTagNode(child.children)
        if(found) {
          return found
        }
      }
    }
  }
  return null
}

默认选中某节点:this.$refs.tree.setCurrentKey(key)
调完接口获取到treeData后一定要用$nextTick去设置,不然死都不会选中

this.currentNode = findFirstTagNode(this.treeData)
this.$nextTick(() => {
	this.currentNode && this.$refs.tree.setCurrentKey(this.currentNode.key)
})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值