elementUI如何動(dòng)態(tài)給el-tree添加子節(jié)點(diǎn)數(shù)據(jù)children詳解
一、需求
有這樣一個(gè)數(shù)據(jù)結(jié)構(gòu)的 tree。

element 的 tree 懶加載是從根上就開始懶加載,但我需要實(shí)現(xiàn)的是已經(jīng)存在一個(gè)層級(jí)的 tree 結(jié)構(gòu),只是在末端點(diǎn)擊的時(shí)候,載入末端以下的列表。
二、實(shí)現(xiàn)
1. tree 的實(shí)例事件 node-click
我們就不能用它的懶加載方法了,而是使用 node-click 這個(gè)事件
<el-tree
ref="treeNav"
:indent="10"
:default-expanded-keys="defaultExpandedKeys"
@node-click="treeNodeClicked"
:data="treeData"
:highlight-current="true"
node-key="id">
</el-tree>
當(dāng)點(diǎn)擊的事件說明:

點(diǎn)擊的時(shí)候你獲取到了:當(dāng)前被點(diǎn)擊節(jié)點(diǎn)的完整數(shù)據(jù),你只需要
- 用其中的
id或者其它自己定義的標(biāo)識(shí)字段,去獲取對(duì)應(yīng)的數(shù)據(jù) - 然后再格式化成
tree的數(shù)據(jù) - 重新賦值給當(dāng)前節(jié)點(diǎn)的
children字段即可
2. tree 的實(shí)例方法:updateKeyChildren
此時(shí)你還需要用到 tree 的另一個(gè)方法: updateKeyChildren。
其作用的就是根據(jù)你提供的節(jié)點(diǎn)的 id,設(shè)置這個(gè)節(jié)點(diǎn)的 children 數(shù)據(jù),剛好是要用到的。

3. 自動(dòng)展示當(dāng)前被點(diǎn)擊的節(jié)點(diǎn)
點(diǎn)擊并插入當(dāng)前節(jié)點(diǎn)的 children 數(shù)據(jù)之后,需要它自動(dòng)展開
只需要設(shè)置 tree 參數(shù)中的 default-expanded-keys 為當(dāng)前節(jié)點(diǎn)的 key 即可
this.defaultExpandedKeys = [nodeData.id]
4. 頁面重新加載后,定位到當(dāng)前的位置
頁面的 url 參數(shù)中保留幾個(gè)參數(shù): projectid devicename index
- 頁面重載之后,用 projectid 來獲取內(nèi)部數(shù)據(jù)
- 添加到已有的樹結(jié)構(gòu)中
- 再使用 tree 組件的 setCurrentNode(nodeKey) 方法來選中該選中的節(jié)點(diǎn)
當(dāng)然,這個(gè)實(shí)現(xiàn)過程還是有點(diǎn)繁瑣的。
頁面重新刷新,其結(jié)果就是:

// 存在 projectId 時(shí),加載對(duì)應(yīng)項(xiàng)目的 preview 信息,并定位到之前的設(shè)備位置
if (this.currentProjectId){
this.getProjectPreviewOf(this.currentProjectId, projectPreview => {
let checkedKey = `project-${this.currentProjectId}:${this.deviceName}`
this.$refs.treeNav.setCurrentKey(checkedKey)
this.SET_CURRENT_DEVICE_LIST(projectPreview[this.deviceName])
})
}
5. 參考代碼
// 根據(jù) projectId 獲取對(duì)應(yīng)項(xiàng)目的 preview
getProjectPreviewOf(nodeData){
areaApi
.previewOfProject({
pid: nodeData.projectInfo.id
})
.then(res => {
let treeDataForPreview = this.getTreeDataFromPreview(res, nodeData.projectInfo.id)
console.log('Tree data for preview: ',treeDataForPreview)
this.$refs.treeNav.updateKeyChildren(nodeData.id, treeDataForPreview)
// 展開當(dāng)前節(jié)點(diǎn)
this.defaultExpandedKeys = [nodeData.id]
// 默認(rèn)展示:當(dāng)前項(xiàng)目的第一個(gè)設(shè)備類型的設(shè)備
this.SET_CURRENT_DEVICE_LIST(treeDataForPreview[0].deviceInfos)
})
},
三、結(jié)果

補(bǔ)充:el-tree子節(jié)點(diǎn)添加el-switch開關(guān)
一開始覺得在子節(jié)點(diǎn)添加開關(guān)是很難的事,主要是想復(fù)雜了,哎,走了好多彎路,一直想的是把子節(jié)點(diǎn)提取出來,然后給賦值,這樣就可以在子節(jié)點(diǎn)顯示出開關(guān),然后被后端一語點(diǎn)醒,不用那么麻煩,他已經(jīng)給了顯示與否的值,只是我一直沒想明白,想來真的是令人尷尬。
效果圖

代碼
<template>
<div>
<el-tree
:data="data"
node-key="id"
default-expand-all
:expand-on-click-node="false"
ref="tree"
>
<!-- v-show="data.swit" -->
<span class="custom-tree-node" slot-scope="{ data }">
<span>{{data.label }}</span>
//根據(jù)data.show!=null是否等于null來判斷顯示與否
<span v-if="data.show!=null"
><el-switch
v-model="data.show"
:active-value="true"
:inactive-value="false"
active-color="#13ce66"
inactive-color="#ff4949"
>
</el-switch
>{{data.show}}
</span>
<!-- -->
</span>
</el-tree>
<el-button @click="handleNodeClick">獲取</el-button>
</div>
</template>
<script>
export default {
data() {
return {
form: {},
// valuettt: false,
data: [
{
label: "aaa",
show: null,
children: [
{
label: "aaa",
show: null,//等于null不顯示開關(guān)
children: [
{
label: "ccc",
show: null,
children: [
{
label: "ccc",
show: null,
children: [
{
label: "三級(jí) 3-1-1",
show: true,//等于true顯示開關(guān)
children: [],
},
],
},
],
},
{
label: "eee",
show: null,
children: [
{
label: "三級(jí) 3-2-2",
show: true,
children: [],
},
],
},
],
},
],
},
{
label: "二級(jí) 3-2",
show: null,
children: [
{
label: "三級(jí) 3-2-1",
show: true,
children: [],
},
],
},
],
defaultProps: {
children: "children",
label: "label",
},
result: [],
};
},
methods: {
handleNodeClick() {
// this.$refs.tree.updateKeyChildren(keys,treeData)//更新node-key的子節(jié)點(diǎn)
this.getAllLeaf(this.data);
// console.log(this.$refs.tree.getCheckedNodes(), "這是數(shù)據(jù)");
},
getAllLeaf(data) {
let result = [];
function getLeaf(data) {
data.forEach((item) => {
if (item.children.length == 0) {
// console.log(item.label,'item.label');
result.push(item.label);
} else {
// console.log(item.children,'item.children');
getLeaf(item.children);
}
});
}
console.log(result, "data");
getLeaf(data);
this.result = result;
return result;
},
},
mounted(){
this.handleNodeClick()
}
};
</script>
<style>
/* .custom-tree-node {
flex: 1;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 14px;
padding-right: 8px;
} */
</style>
由于我之前還想獲取子節(jié)點(diǎn),所以代碼中還有獲取子節(jié)點(diǎn)的方法。

總結(jié)
到此這篇關(guān)于elementUI如何動(dòng)態(tài)給el-tree添加子節(jié)點(diǎn)數(shù)據(jù)children的文章就介紹到這了,更多相關(guān)elementUI給el-tree添加子節(jié)點(diǎn)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue項(xiàng)目中使用fontawesome圖標(biāo)庫的方法
fontawesome的圖標(biāo)有免費(fèi)版和專業(yè)版,本文主要使用free版本,一般free版本的圖標(biāo)夠用,free圖標(biāo)又劃分為三個(gè)圖標(biāo)庫,主要有實(shí)心圖標(biāo)solid、常規(guī)圖標(biāo)regular及品牌圖標(biāo)brand,根據(jù)需求去下載對(duì)應(yīng)的圖標(biāo)庫,無須全部下載,對(duì)vue?fontawesome圖標(biāo)庫相關(guān)知識(shí)感興趣的朋友一起看看吧2023-12-12
vue使用監(jiān)聽實(shí)現(xiàn)全選反選功能
最近做的項(xiàng)目用到了全選全不選功能,于是我就自己動(dòng)手寫了一個(gè),基于vue使用監(jiān)聽實(shí)現(xiàn)全選反選功能,具體實(shí)例代碼大家參考下本文2018-07-07
基于vue實(shí)現(xiàn)滾動(dòng)條滾動(dòng)到指定位置對(duì)應(yīng)位置數(shù)字進(jìn)行tween特效
這篇文章主要介紹了基于vue實(shí)現(xiàn)滾動(dòng)條滾動(dòng)到指定位置對(duì)應(yīng)位置數(shù)字進(jìn)行tween特效,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
vue中設(shè)置echarts寬度自適應(yīng)的代碼步驟
這篇文章主要介紹了vue中設(shè)置echarts寬度自適應(yīng)的問題及解決方案,常常需要做到echarts圖表的自適應(yīng),一般是根據(jù)頁面的寬度做對(duì)應(yīng)的適應(yīng),本文記錄一下設(shè)置echarts圖表的自適應(yīng)的步驟,需要的朋友可以參考下2022-09-09
vue-electron項(xiàng)目創(chuàng)建記錄及問題小結(jié)解決方案
這篇文章主要介紹了vue-electron項(xiàng)目創(chuàng)建記錄及注意事項(xiàng),本文給大家分享了運(yùn)行項(xiàng)目報(bào)錯(cuò)的問題小結(jié)及多種解決方案,需要的朋友可以參考下2024-03-03
在Vue項(xiàng)目中引入Echarts繪制K線圖的方法技巧
在Vue項(xiàng)目開發(fā)中,數(shù)據(jù)可視化是一項(xiàng)重要的任務(wù),Echarts是一個(gè)由百度開源的數(shù)據(jù)可視化庫,提供了豐富的圖表類型和強(qiáng)大的交互功能,其中,K線圖常用于展示金融數(shù)據(jù)的走勢(shì),本文將詳細(xì)介紹如何在Vue項(xiàng)目中引入Echarts并繪制K線圖,需要的朋友可以參考下2025-04-04
Vue.js實(shí)戰(zhàn)之組件之間的數(shù)據(jù)傳遞
這篇文章主要介紹了Vue.js實(shí)戰(zhàn)之組件之間的數(shù)據(jù)傳遞的相關(guān)資料,文中通過示例代碼和圖文介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04

