vue實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)樣式和功能的實(shí)例代碼
一、主要運(yùn)用element封裝的控件并封裝成組件運(yùn)用,如圖所示

代碼如圖所示:

下面是子組件的代碼:
<template>
<ul class="l_tree">
<li class="l_tree_branch" v-for="item in model" :key="item.id">
<div class="l_tree_click">
<button type="button" class="l_tree_children_btn" v-if="item.children" @click="toggle(item)">{{ !item.show ? '-' : '+' }}</button>
<span class="l_folder" @click="clickSize(item.id)" :class="current === item.id ? 'current' : 'currentSize'">{{ item.name }}</span>
</div>
<ew-tree
v-show="!item.show"
:model="item.children"
:current="current"
@change="changeCurrent"></ew-tree>
</li>
</ul>
</template>
<script>
export default {
name: 'EwTree',
props: {
model: {
type: Array,
default() {
return []
}
},
current: {
type: String,
default: ''
}
},
methods: {
toggle(item) {
console.log(item)
var idx = this.model.indexOf(item)
this.$set(this.model[idx], 'show', !item.show)
},
clickSize(id) {
console.log(1, id)
this.$emit('change', id)
},
changeCurrent(id) {
this.clickSize(id)
}
},
mounted() {
}
}
</script>
<style lang="less" scoped>
*{
box-sizing: border-box;
margin: 0;padding: 0;
}
*:before,*:after{
box-sizing: border-box;
}
ul,
li {
list-style: none;
}
.current{
color: #e9c309 !important
}
.l_tree_container {
width: 100%;
height: 100%;
box-shadow: 0 0 3px #ccc;
margin: 13px;
position: relative;
}
.l_tree {
width: calc(100%);
padding-left: 15px;
}
.l_tree_branch {
width: 100%;
height: 100%;
display: block;
padding: 13px;
position: relative;
}
.l_tree_branch .l_tree_children_btn {
width: 12px;
height: 12px;
background-color: #515a68;
font-size: 8px;
text-align: center;
color: #bbbec1;
outline: none;
border: 0;
cursor: pointer;
border: 1px solid #bbbec1;
line-height: 11px;
margin-left: 5px;
}
ul.l_tree:before {
content: '';
border-left: 1px dashed #999999;
height: calc(100% - 24px);
position: absolute;
left: 10px;
top: 0px;
}
.l_tree,
.l_tree_branch {
position: relative;
}
.l_tree_branch::after {
content: '';
width: 18px;
height: 0;
border-bottom: 1px dashed #bbbec1;
position: absolute;
right: calc(100% - 10px);
top: 24px;
left: -5px;
}
.l_tree_container>.l_tree::before,
.l_tree_container>.l_tree>.l_tree_branch::after {
display: none;
}
.l_folder {
font-size:11px;
margin-left: 5px;
display: inline;
color: #bbbec1;
cursor: pointer;
}
</style>
主要難點(diǎn)是:current傳值問(wèn)題,所以current綁定在父組件

父組件中的值和方法:


當(dāng)然在運(yùn)行npm時(shí)是需要安裝npm install ewtree -S,實(shí)現(xiàn)組件化
最終效果如下:

總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)樣式和功能的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Vue?elementUI實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)表格與懶加載
- vue實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)增刪改查的示例代碼
- vue開(kāi)發(fā)樹(shù)形結(jié)構(gòu)組件(組件遞歸)
- vue Element-ui表格實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)表格
- vue實(shí)現(xiàn)的樹(shù)形結(jié)構(gòu)加多選框示例
- vue樹(shù)形結(jié)構(gòu)獲取鍵值的方法示例
- Vue.js 遞歸組件實(shí)現(xiàn)樹(shù)形菜單(實(shí)例分享)
- vuejs使用遞歸組件實(shí)現(xiàn)樹(shù)形目錄的方法
- 用 Vue.js 遞歸組件實(shí)現(xiàn)可折疊的樹(shù)形菜單(demo)
- vue遞歸組件實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)
相關(guān)文章
Vue項(xiàng)目保存代碼之后頁(yè)面自動(dòng)更新問(wèn)題
這篇文章主要介紹了Vue項(xiàng)目保存代碼之后頁(yè)面自動(dòng)更新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue3實(shí)現(xiàn)獲取驗(yàn)證碼按鈕倒計(jì)時(shí)效果
這篇文章主要介紹了Vue3實(shí)現(xiàn)獲取驗(yàn)證碼按鈕倒計(jì)時(shí)效果,用戶點(diǎn)擊獲取驗(yàn)證碼按鈕,發(fā)送請(qǐng)求給后端,按鈕失效,并且開(kāi)始倒計(jì)時(shí)60秒;在此期間,用戶無(wú)法再次點(diǎn)擊按鈕,即使用戶刷新頁(yè)面,倒計(jì)時(shí)依然存在,直到倒計(jì)時(shí)完畢,按鈕恢復(fù),感興趣的小伙伴跟著小編一起來(lái)看看吧2024-10-10
vue限制輸入框只能輸入8位整數(shù)和2位小數(shù)的代碼
這篇文章主要介紹了vue限制輸入框只能輸入8位整數(shù)和2位小數(shù),文中我們使用v-model加watch 實(shí)現(xiàn)這一個(gè)功能,代碼簡(jiǎn)單易懂,需要的朋友可以參考下2019-11-11
vscode搭建vue環(huán)境完整圖文教程(適合新手小白)
Vue框架的優(yōu)秀設(shè)計(jì)和強(qiáng)大的生態(tài)系統(tǒng)成為了越來(lái)越多開(kāi)發(fā)者選擇Vue的原因,在實(shí)際項(xiàng)目過(guò)程中一個(gè)高效的開(kāi)發(fā)環(huán)境能夠大大提高開(kāi)發(fā)效率,這篇文章主要給大家介紹了關(guān)于vscode搭建vue環(huán)境的相關(guān)資料,需要的朋友可以參考下2023-10-10
vue的簡(jiǎn)介及@vue/cli?腳手架的使用示例
vue 是一個(gè) 漸進(jìn)式的javascript框架,腳手架是一個(gè)通用概念,幫助搭建項(xiàng)目的工具,本文以vue2為例結(jié)合實(shí)例代碼給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧2022-12-12
Vue3 Element-plus el-menu無(wú)限級(jí)菜單組件封裝過(guò)程
對(duì)于element中提供給我們的el-menu組件最多可以實(shí)現(xiàn)三層嵌套,如果多一層數(shù)據(jù)只能自己通過(guò)變量去加一層,如果加了兩層、三層這種往往是行不通的,所以只能進(jìn)行封裝,這篇文章主要介紹了Vue3 Element-plus el-menu無(wú)限級(jí)菜單組件封裝,需要的朋友可以參考下2023-04-04
Vue.js中this如何取到data和method里的屬性詳解
methods屬性是一個(gè)對(duì)象,通常我們會(huì)在這個(gè)對(duì)象中定義很多的方法,下面這篇文章主要給大家介紹了關(guān)于Vue.js中this如何取到data和method里的屬性,需要的朋友可以參考下2022-12-12

