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為一個數(shù)組,在data中給treeData一個初始值:
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:需要特別說明的是,我目前的代碼暫時只支持這兩種樣式,即:
1父節(jié)點(diǎn)-1子節(jié)點(diǎn)-1/多孫節(jié)點(diǎn),或是1父節(jié)點(diǎn)-多子節(jié)點(diǎn)-1孫節(jié)點(diǎn),樣式是通過jq去判斷修改的,以后有時間的話再去研究優(yōu)化爭取可復(fù)用性強(qiáng)一些。希望對大家能有所幫助。
總結(jié)
以上所述是小編給大家介紹的vue組件橫向樹實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(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)一個樹組件的示例
相關(guān)文章
vue 如何添加全局函數(shù)或全局變量以及單頁面的title設(shè)置總結(jié)
本篇文章主要介紹了vue 如何添加全局函數(shù)或全局變量以及單頁面的title設(shè)置總結(jié),非常具有實(shí)用價值,需要的朋友可以參考下2017-06-06
webpack + vue 打包生成公共配置文件(域名) 方便動態(tài)修改
這篇文章主要介紹了webpack + vue 打包生成公共配置文件(域名) 方便動態(tài)修改,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
Vue 使用v-model實(shí)現(xiàn)控制子組件顯隱效果
v-model 可以實(shí)現(xiàn)雙向綁定的效果,允許父組件控制子組件的顯示/隱藏,同時允許子組件自己控制自身的顯示/隱藏,本文給大介紹Vue 使用v-model實(shí)現(xiàn)控制子組件顯隱,感興趣的朋友一起看看吧2023-11-11
vue2自定義組件通過rollup配置發(fā)布到npm的詳細(xì)步驟
這篇文章主要介紹了vue2自定義組件通過rollup配置發(fā)布到npm,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
深入解析Vue中的this.$forceUpdate()的使用
this.$forceUpdate()?是一個重要的實(shí)例方法,本文主要介紹了深入解析Vue中的this.$forceUpdate()的使用,具有一定的參考價值,感興趣的可以了解一下2024-07-07
vue + node如何通過一個Txt文件批量生成MP3并壓縮成Zip
這篇文章主要介紹了vue + node如何通過一個Txt文件批量生成MP3并壓縮成Zip的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
詳解swiper在vue中的應(yīng)用(以3.0為例)
這篇文章主要介紹了詳解swiper在vue中的應(yīng)用(以3.0為例),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
vue跳轉(zhuǎn)頁面攜帶參數(shù)并且立即執(zhí)行方法
這篇文章主要介紹了vue跳轉(zhuǎn)頁面攜帶參數(shù)并且立即執(zhí)行方法,首先定義跳轉(zhuǎn)函數(shù),結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10
Vue彈窗的兩種實(shí)現(xiàn)方式實(shí)例詳解
這篇文章主要介紹了Vue彈窗的兩種實(shí)現(xiàn)方式,一種使用.sync修飾符另一種使用v-model,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08

