vue組件橫向樹實(shí)現(xiàn)代碼
將之前的用css3+jq實(shí)現(xiàn)的橫向樹樣式簡單封裝成組件使用到vue項(xiàng)目中,文件名為transverseTree.vue
代碼:
<template> <div class="tree"> <ul v-if="treeData && treeData.length"> <li v-for="(column,index) in treeData"> <span class="root">{{column.name}}</span> <ul v-if="column.children && column.children.length"> <li v-for="(childrenColumn,index) in column.children"> <span>{{childrenColumn.name}}</span> <ul v-if="childrenColumn.children && childrenColumn.children.length"> <li v-for="(grandChildrenColumn,index) in childrenColumn.children"> <span>{{grandChildrenColumn.name}}</span> </li> </ul> </li> </ul> </li> </ul> </div> </template> <script> export default { name: 'transverseTree', props: { treeData:{ type:Array, default:[] } }, methods: { editDom(){ if($('.root').siblings('ul').children('li').length==1){ let num = 26*($('.root').siblings('ul').children('li').find('li').length-1); $('.root').css({ 'top': num }); $('.root').siblings('ul').children('li').css({ 'top': num }); $('.root').siblings('ul').find('ul').css({ 'top': -num }); if($('.root').siblings('ul').find('li').length > 1){ $('.root').siblings('ul').children('li').children('span').addClass('hasChild'); } }else{ $('.root').css({ 'top': 26 * ($('.root').siblings('ul').children('li').length - 1) }); } } }, mounted() { this.$nextTick(()=>{ this.editDom(); }); } }; </script> <style scope> .tree{ position: relative; margin: -16px -16px 0; min-height: 400px; padding-left: 11px; overflow: auto; } .tree ul{ width: 210px; height: 100%; position: absolute; } .tree ul ul{ left: 226px; top: 0; } .tree li{ float: left; list-style-type: none; position: relative; padding: 16px 5px 0 5px; } .tree li span{ position: relative; display: inline-block; width: 200px; height: 36px; background: #F0F0F5; border-radius: 4px; text-decoration: none; color: #2D2D2D; font-size: 14px; line-height: 36px; text-align: center; } .tree li::before{ box-sizing:inherit; content: ''; position: absolute; top: 33px; left: -7px; border-top: 2px solid #D2D2D7; width: 12px; } .tree li::after{ box-sizing:inherit; content: ''; position: absolute; top: 8px; left: -9px; height: 100%; border-left: 2px solid #D2D2D7; } .tree li:first-child::after{ height: 51%; border-left: 2px solid #D2D2D7; border-top: 2px solid #D2D2D7; top: 33px; width: 1px; border-top-left-radius: 4px; } .tree li:last-child::after{ height: 25px; border-left: 2px solid #D2D2D7; border-bottom: 2px solid #D2D2D7; top: 8px; width: 1px; border-bottom-left-radius: 4px; } .tree li:only-child::after, .tree li:only-child::before{ display: none; } .tree ul ul li:only-child::before{ display: inline-block; } .tree ul ul li:only-child span::before{ display: inline-block; } .tree li:only-child span.root::before,.tree li:only-child span.hasChild::before{ content: ''; position: absolute; top: 17px; right: -14px; border-top: 2px solid #D2D2D7; width: 14px; } .tree ul ul ul li:only-child span::before{ content: ''; position: absolute; top: 17px; left: -26px; border-top: 2px solid #D2D2D7; width: 26px; } </style>
在父組件中使用import引入該組件:
import transverseTree from './transverseTree'
注冊組件:
components: { ifbpInfolistCard,transverseTree },
在template中使用:
<transverse-tree :treeData='treeData'></transverse-tree>
其中,treeData為一個(gè)數(shù)組,在data中給treeData一個(gè)初始值:
treeData: [ {name:'報(bào)表名稱1', children:[ {name:'功能名稱1', children:[ {name:'磁貼名稱1'} ]}, {name:'功能名稱2', children:[ {name:'磁貼名稱1'} ]}, {name:'功能名稱3', children:[ {name:'磁貼名稱1'} ]}, ]} ]
實(shí)現(xiàn)效果:
ps:需要特別說明的是,我目前的代碼暫時(shí)只支持這兩種樣式,即:
1父節(jié)點(diǎn)-1子節(jié)點(diǎn)-1/多孫節(jié)點(diǎn),或是1父節(jié)點(diǎn)-多子節(jié)點(diǎn)-1孫節(jié)點(diǎn),樣式是通過jq去判斷修改的,以后有時(shí)間的話再去研究優(yōu)化爭取可復(fù)用性強(qiáng)一些。希望對大家能有所幫助。
總結(jié)
以上所述是小編給大家介紹的vue組件橫向樹實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- vue+element樹組件 實(shí)現(xiàn)樹懶加載的過程詳解
- vue遞歸組件實(shí)戰(zhàn)之簡單樹形控件實(shí)例代碼
- Vue.js遞歸組件實(shí)現(xiàn)組織架構(gòu)樹和選人功能
- Vue使用zTree插件封裝樹組件操作示例
- Vue遞歸組件+Vuex開發(fā)樹形組件Tree--遞歸組件的簡單實(shí)現(xiàn)
- vue用遞歸組件寫樹形控件的實(shí)例代碼
- vue文件樹組件使用詳解
- vuejs實(shí)現(xiàn)遞歸樹型菜單組件
- 用 Vue.js 遞歸組件實(shí)現(xiàn)可折疊的樹形菜單(demo)
- Vue.js遞歸組件構(gòu)建樹形菜單
- 基于Vue制作組織架構(gòu)樹組件
- 使用Vue實(shí)現(xiàn)一個(gè)樹組件的示例
相關(guān)文章
Vue使用watch監(jiān)聽一個(gè)對象中的屬性的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue使用watch監(jiān)聽一個(gè)對象中的屬性的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05Babel自動(dòng)生成Attribute文檔實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Babel自動(dòng)生成Attribute文檔實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11淺談Vue中的this.$store.state.xx.xx
這篇文章主要介紹了Vue中的this.$store.state.xx.xx用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09vue3快速實(shí)現(xiàn)主題切換功能的步驟詳解
本文介紹一種基于css變量的主題切換實(shí)現(xiàn)方式,這種是最簡單,最直接,最容易理解的方式,實(shí)現(xiàn)的原理就是定義不同的HTML根標(biāo)簽元素的樣式,通過data屬性來區(qū)分不同主題css變量樣式,感興趣的朋友可以參考下2024-06-06vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式
這篇文章主要介紹了vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07