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)擊的事件說(shuō)明:
點(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. 頁(yè)面重新加載后,定位到當(dāng)前的位置
頁(yè)面的 url 參數(shù)中保留幾個(gè)參數(shù): projectid
devicename
index
- 頁(yè)面重載之后,用 projectid 來(lái)獲取內(nèi)部數(shù)據(jù)
- 添加到已有的樹結(jié)構(gòu)中
- 再使用 tree 組件的 setCurrentNode(nodeKey) 方法來(lái)選中該選中的節(jié)點(diǎn)
當(dāng)然,這個(gè)實(shí)現(xiàn)過(guò)程還是有點(diǎn)繁瑣的。
頁(yè)面重新刷新,其結(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)提取出來(lái),然后給賦值,這樣就可以在子節(jié)點(diǎn)顯示出開關(guān),然后被后端一語(yǔ)點(diǎn)醒,不用那么麻煩,他已經(jīng)給了顯示與否的值,只是我一直沒想明白,想來(lái)真的是令人尷尬。
效果圖
代碼
<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來(lái)判斷顯示與否 <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)文章
vue3+vite中開發(fā)環(huán)境與生產(chǎn)環(huán)境全局變量配置指南
最近在使用vite生成項(xiàng)目,這篇文章主要給大家介紹了關(guān)于vue3+vite中開發(fā)環(huán)境與生產(chǎn)環(huán)境全局變量配置的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08ant-design-vue中的select選擇器,對(duì)輸入值的進(jìn)行篩選操作
這篇文章主要介紹了ant-design-vue中的select選擇器,對(duì)輸入值的進(jìn)行篩選操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10優(yōu)選七個(gè)用于vue開發(fā)的JS庫(kù)
這篇文章主要介紹了JavaScript優(yōu)選七個(gè)用于vue開發(fā)庫(kù),借助開源庫(kù)加速Vue項(xiàng)目的開發(fā)進(jìn)度是現(xiàn)代前端開發(fā)比較常見的方式,平常收集一些JavaScript庫(kù)介紹,在遇到需要的時(shí)候可以信手拈來(lái)2023-02-02element-ui中樣式覆蓋問(wèn)題的方法總結(jié)
我們?cè)谑褂胑lement-ui的時(shí)候經(jīng)常會(huì)遇到需要修改組件默認(rèn)樣式,下面這篇文章主要給大家介紹了關(guān)于element-ui中樣式覆蓋問(wèn)題的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03vue移動(dòng)端使用appClound拉起支付寶支付的實(shí)現(xiàn)方法
這篇文章主要介紹了vue移動(dòng)端使用appClound拉起支付寶支付的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Vue使用枚舉類型實(shí)現(xiàn)HTML下拉框步驟詳解
本文分步驟給大家介紹了Vue使用枚舉類型實(shí)現(xiàn)HTML下拉框的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02vue子組件實(shí)時(shí)獲取父組件的數(shù)據(jù)實(shí)現(xiàn)
本文主要介紹了vue子組件實(shí)時(shí)獲取父組件的數(shù)據(jù)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12