vue el-tree 默認展開第一個節點的實現代碼
vue 的樹形控件 el-tree 可以用來方便地實現樹形控件,但是官方文檔中,關于控件的默認展開只有默認展開全部或者默認全部關閉,如下所示:
對于指定節點的展開,需要指定其id,從而通過 default-expanded-keys 設置默認展開的節點。對于后臺返回的數據,默認展開其第一層的第一個,其實很簡單:對于獲取到的后臺數據,將其第一層節點添加到數組中,將 default-expanded-keys 綁定數組,從而設置默認展開的節點。實際應用:默認展開第一層節點中的第一個節點:
<template> <section> <!-- 機構類型編碼表 --> <el-row align='left'> <div class=’treeClass’> <el-tree :data='treeData' :props='defaultProps' @node-click='handleNodeClick' highlight-current node-key='id' :default-expanded-keys='treeExpandData'> </el-tree> </div> </el-row> </section></template><script>export default { data() { return { treeData:[], //后臺返回的tree樹列表 treeExpandData:[], //自己定義的用于接收tree樹id的數組 provincialCenterId:’’, defaultProps: { children: ’item’, label: ’name’, }, } }, created(){ this.getEquipmentList() }, methods: { // 獲取樹形結構默認展開節點 getRoleTreeRootNode(provincialCenterId) { this.treeExpandData.push(provincialCenterId) }, //獲取tree樹列表 getEquipmentList: function(params){ this.listLoading = true this.$api.ckApi.treeList({typeTag:true}).then((res)=>{ if(res.code==200){ this.treeData = res.resultDownload; this.provincialCenterId = this.treeData[0].id //默認展開第一個節點 this.getRoleTreeRootNode(this.provincialCenterId) this.listLoading = false }else{ this.$message.error(res) } }) }, }</script>
效果圖:
總結
到此這篇關于vue el-tree 默認展開第一個節點的實現代碼的文章就介紹到這了,更多相關vue el-tree默認展開節點內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
