Bootstrap

【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)
})
;