vue el-tree 默認(rèn)展開第一個節(jié)點的實現(xiàn)代碼
vue 的樹形控件 el-tree 可以用來方便地實現(xiàn)樹形控件,但是官方文檔中,關(guān)于控件的默認(rèn)展開只有默認(rèn)展開全部或者默認(rèn)全部關(guān)閉,如下所示:
對于指定節(jié)點的展開,需要指定其id,從而通過 default-expanded-keys 設(shè)置默認(rèn)展開的節(jié)點。對于后臺返回的數(shù)據(jù),默認(rèn)展開其第一層的第一個,其實很簡單:對于獲取到的后臺數(shù)據(jù),將其第一層節(jié)點添加到數(shù)組中,將 default-expanded-keys 綁定數(shù)組,從而設(shè)置默認(rèn)展開的節(jié)點。實際應(yīng)用:默認(rèn)展開第一層節(jié)點中的第一個節(jié)點:
<template> <section> <!-- 機構(gòu)類型編碼表 --> <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的數(shù)組 provincialCenterId:’’, defaultProps: { children: ’item’, label: ’name’, }, } }, created(){ this.getEquipmentList() }, methods: { // 獲取樹形結(jié)構(gòu)默認(rèn)展開節(jié)點 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 //默認(rèn)展開第一個節(jié)點 this.getRoleTreeRootNode(this.provincialCenterId) this.listLoading = false }else{ this.$message.error(res) } }) }, }</script>
效果圖:
總結(jié)
到此這篇關(guān)于vue el-tree 默認(rèn)展開第一個節(jié)點的實現(xiàn)代碼的文章就介紹到這了,更多相關(guān)vue el-tree默認(rèn)展開節(jié)點內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
