vue el-tree 默認(rèn)展開第一個(gè)節(jié)點(diǎn)的實(shí)現(xiàn)代碼
vue 的樹形控件 el-tree 可以用來方便地實(shí)現(xiàn)樹形控件,但是官方文檔中,關(guān)于控件的默認(rèn)展開只有默認(rèn)展開全部或者默認(rèn)全部關(guān)閉,如下所示:
對(duì)于指定節(jié)點(diǎn)的展開,需要指定其id,從而通過 default-expanded-keys 設(shè)置默認(rèn)展開的節(jié)點(diǎn)。對(duì)于后臺(tái)返回的數(shù)據(jù),默認(rèn)展開其第一層的第一個(gè),其實(shí)很簡(jiǎn)單:對(duì)于獲取到的后臺(tái)數(shù)據(jù),將其第一層節(jié)點(diǎn)添加到數(shù)組中,將 default-expanded-keys 綁定數(shù)組,從而設(shè)置默認(rèn)展開的節(jié)點(diǎn)。實(shí)際應(yīng)用:默認(rèn)展開第一層節(jié)點(diǎn)中的第一個(gè)節(jié)點(diǎn):
<template> <section> <!-- 機(jī)構(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:[], //后臺(tái)返回的tree樹列表 treeExpandData:[], //自己定義的用于接收tree樹id的數(shù)組 provincialCenterId:’’, defaultProps: { children: ’item’, label: ’name’, }, } }, created(){ this.getEquipmentList() }, methods: { // 獲取樹形結(jié)構(gòu)默認(rèn)展開節(jié)點(diǎn) 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)展開第一個(gè)節(jié)點(diǎn) this.getRoleTreeRootNode(this.provincialCenterId) this.listLoading = false }else{ this.$message.error(res) } }) }, }</script>
效果圖:
總結(jié)
到此這篇關(guān)于vue el-tree 默認(rèn)展開第一個(gè)節(jié)點(diǎn)的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)vue el-tree默認(rèn)展開節(jié)點(diǎn)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. css進(jìn)階學(xué)習(xí) 選擇符2. HTML <!DOCTYPE> 標(biāo)簽3. css代碼優(yōu)化的12個(gè)技巧4. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案5. 使用css實(shí)現(xiàn)全兼容tooltip提示框6. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)7. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera8. 詳解瀏覽器的緩存機(jī)制9. HTML DOM setInterval和clearInterval方法案例詳解10. 告別AJAX實(shí)現(xiàn)無刷新提交表單
