- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- VUE 無(wú)限層級樹(shù)形數據結構顯示的實(shí)現
在做項目中,會(huì )遇到一些樹(shù)形的數據結構,常用在左側菜單導航,或者評論引用等地方,這種數據結構有個(gè)特點(diǎn)是不知道它會(huì )嵌套多少層,所以用template去展示這樣的數據時(shí)就有點(diǎn)棘手,這篇文章梳理兩種展示這種數據結構的方法。
文章中用到的數據是下面這個(gè):
mainData: { value: "root", children:[{ value: "層級1-1", children:[{ value: "層級2-1", children:[{ value: "層級3-1", children:[] }] },{ value: "層級2-2", children:[] }] },{ value: "層級1-2", children:[] }] }
也就是下面這個(gè)樣子。
第一種是組件遞歸調用自己的方式,創(chuàng )建一個(gè)組件,該組件在引用自己去展示children的數據,子組件如下:
<template> <div> <div class="demo"> {{treeData.value}} <tree-comp v-for="(item, index) in treeData.children" :treeData="item"></tree-comp> </div> </div> </template> <script> export default { name: 'treeComp', props:{ treeData: { default: function(){ return {} } } }, mounted(){}, methods:{} } </script> <style lang="less" scoped> .demo{padding:5px 0;margin:1px 10px;text-align: left;font-size:16px;max-width:500px;border-left:1px dashed #999; &:before{content:'--';display: inline-block;padding:0 4px;} } </style>
然后創(chuàng )建父組件,父組件使用子組件,并將數據傳入子組件。
<template> <tree-comp :treeData="mainData"></tree-comp> </template> <script> export default { name: 'treeMain', data () { return { mainData: { value: "root", children:[ { value: "層級1-1", children:[{ value: "層級2-1", children:[{ value: "層級3-1", children:[] }] },{ value: "層級2-2", children:[] }] },{ value: "層級1-2", children:[] } ] } } }, components:{ "tree-comp": () => import('./TreeComp') }, mounted(){}, methods:{} } </script>
關(guān)于遞歸組件的內容,在官方文檔里是有提到的-->
除了使用組件的方式,也可以使用vue的render方法,去利用JavaScript 的完全編程的能力,實(shí)現遞歸處理樹(shù)形數據,從而展示出無(wú)限層級樹(shù)。如下:
<template> <tree-comp :treeData="mainData"></tree-comp> </template> <script> export default { name: 'treeRender', data () { return { mainData: { value: "root", children:[ { value: "層級1-1", children:[{ value: "層級2-1", children:[{ value: "層級3-1", children:[] }] },{ value: "層級2-2", children:[] }] },{ value: "層級1-2", children:[] } ] } } }, components:{ treeComp:{ functional: true, props: {treeData: Object}, render(h, {props: {treeData = {}}}) { const creatNode = (node)=>{ if(node.children && node.children.length > 0){ let hArr = node.children.map(item=>{ return creatNode(item) }) return h('div', {class:'demo'}, [node.value, hArr]) }else{ return h('div', {class:'demo'}, [node.value]) } } return creatNode(treeData) }, } }, mounted(){}, methods:{} } </script> <style lang="less" scoped> .demo{padding:5px 0;margin:1px 10px;text-align: left;font-size:16px;max-width:500px;border-left:1px dashed #999; &:before{content:'--';display: inline-block;padding:0 4px;} } </style>
其中最核心的就是在render方法里,creatNode方法用遞歸的方式,深度優(yōu)先遍歷樹(shù)狀數據,生成vnode,然后渲染出了頁(yè)面。
到此這篇關(guān)于VUE 無(wú)限層級樹(shù)形數據結構顯示的實(shí)現的文章就介紹到這了,更多相關(guān)VUE 無(wú)限層級樹(shù)形結構內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站