VUE使用vue-tree-color組件實(shí)現(xiàn)組織架構(gòu)圖并可以動(dòng)態(tài)更新數(shù)據(jù)的效果
npm
# use npm npm install vue-tree-color
安裝loader
npm install --save-dev less less-loader
導(dǎo)入插件
import Vue from 'vue' import Vue2OrgTree from 'vue-tree-color' Vue.use(Vue2OrgTree)
基本使用
開始
因?yàn)橐呀?jīng)安裝過了組件,所以可以直接使用,在vue頁(yè)面中,直接使用組件標(biāo)簽,動(dòng)態(tài)綁定data數(shù)據(jù)(data數(shù)據(jù)為遞歸數(shù)據(jù)即可)
<vue2-org-tree :data="data"/>
...
datas:{
id:0,
label:'一級(jí)',
children:[
{
id:11,
label:'二級(jí)1'
}
]
}data數(shù)據(jù)放入頁(yè)面中
其中,data數(shù)據(jù)中,id 每個(gè)元素不同的ID ,label為name, children為自己的子集數(shù)據(jù)

排列方式
上面圖片是默認(rèn)排列方式,其實(shí)還有一種水平排列方式
# 只需要加上 horizontal 即可 <vue2-org-tree :data="datas" :horizontal="true" />
效果如下

折疊展示
添加一個(gè)屬性 collapsable,并添加一個(gè)組件自帶方法
<vue2-org-tree :data="data" :horizontal="true" collapsable @on-expand="onExpand" />
...
methods: {
collapse(list) {
var _this = this
list.forEach(function(child) {
if (child.expand) {
child.expand = false
}
child.children && _this.collapse(child.children)
})
},
onExpand(e, data) {
if ('expand' in data) {
data.expand = !data.expand
if (!data.expand && data.children) {
this.collapse(data.children)
}
} else {
this.$set(data, 'expand', true)
}
}
}效果如下

點(diǎn)擊節(jié)點(diǎn)
添加一個(gè)方法 on-node-click
<vue2-org-tree :data="data" :horizontal="true" collapsable @on-expand="onExpand" @on-node-click="onNodeHandle" />
...
onNodeHandle(e, data) {
// e是節(jié)點(diǎn)數(shù)據(jù)
console.log(e)
// data是渲染在節(jié)點(diǎn)上的數(shù)據(jù)
console.log(data)
},已上為使用vue-tree-color組件實(shí)現(xiàn)組織架構(gòu)圖,接下來實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)加載
數(shù)據(jù)動(dòng)態(tài)加載
<template>
<div >
<vue2-org-tree :data="datas" @on-node-click="onNodeHandle" />
</div>
</template>
<script>
export default {
data () {
return {
datas:{
id:0,
label:'一級(jí)',
children:[
{
id:11,
label:'二級(jí)1'
}
]
}
}
},
methods: {
onNodeHandle(e, data) {
let newChild = [
{
id: 111,
label: '三級(jí)1'
},
{
id: 112,
label: '三級(jí)2'
},{
id: 113,
label: '三級(jí)3'
}
]
let targetNode = this.datas.children.find(node => node.id === 11);
if (targetNode) {
// 使用$set方法添加子節(jié)點(diǎn)
this.$set(targetNode, 'children', newChild); // 更新數(shù)據(jù)需要vue的響應(yīng)式系統(tǒng)能捕獲到
}
}
}
}
</script>其中實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)的加載關(guān)鍵在于確保數(shù)據(jù)更新是在 Vue 的響應(yīng)式系統(tǒng)能夠捕獲到的情況下進(jìn)行的。例如,如果數(shù)據(jù)是通過異步請(qǐng)求獲取的,要確保在請(qǐng)求成功后,正確地更新treeData。如果在更新數(shù)據(jù)時(shí),沒有遵循 Vue 的響應(yīng)式規(guī)則,比如直接修改數(shù)組的索引而不是使用 Vue 提供的數(shù)組變異方法(如push、splice等)或者ref、reactive的更新方法,組件可能無法正確更新。例如,不要這樣更新數(shù)組this.treeData[0]=newValue(這不會(huì)觸發(fā)響應(yīng)式更新),而應(yīng)該使用this.treeData.splice(0, 1, newValue)或者如果treeData是ref定義的,treeData.value.push(newValue)
動(dòng)態(tài)數(shù)據(jù)效果圖

到此這篇關(guān)于VUE使用vue-tree-color組件實(shí)現(xiàn)組織架構(gòu)圖并可以動(dòng)態(tài)更新數(shù)據(jù)的效果的文章就介紹到這了,更多相關(guān)vue 使用vue-tree-color組織架構(gòu)圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue如何實(shí)現(xiàn)將后端返回二進(jìn)制文件在瀏覽器自動(dòng)下載
Vue項(xiàng)目開發(fā)中,遇到界面下載功能時(shí),前端如何實(shí)現(xiàn)將后端返回二進(jìn)制文件在瀏覽器自動(dòng)下載呢,本文就來和大家聊聊具體的解決方法吧2024-11-11
vue 對(duì)象添加或刪除成員時(shí)無法實(shí)時(shí)更新的解決方法
這篇文章主要介紹了vue 對(duì)象添加或刪除成員時(shí)無法實(shí)時(shí)更新的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
vue 解決移動(dòng)端彈出鍵盤導(dǎo)致頁(yè)面fixed布局錯(cuò)亂的問題
今天小編就為大家分享一篇vue 解決移動(dòng)端彈出鍵盤導(dǎo)致頁(yè)面fixed布局錯(cuò)亂的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
關(guān)于Vue-extend和VueComponent問題小結(jié)
這篇文章主要介紹了Vue-extend和VueComponent問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
element?ui動(dòng)態(tài)側(cè)邊菜單欄及頁(yè)面布局實(shí)現(xiàn)方法
后臺(tái)管理系統(tǒng)經(jīng)常會(huì)使用到一個(gè)左側(cè)菜單欄,右側(cè)Tab頁(yè)的頁(yè)面顯示結(jié)構(gòu),這篇文章主要給大家介紹了關(guān)于element?ui動(dòng)態(tài)側(cè)邊菜單欄及頁(yè)面布局實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-09-09
vue項(xiàng)目回到頂部的兩種超簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目回到頂部的兩種超簡(jiǎn)單實(shí)現(xiàn)方法,?頁(yè)面切換回到頂部是一個(gè)很常見的功能,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10

