vue實(shí)現(xiàn)樹形結(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傳值問題,所以current綁定在父組件
父組件中的值和方法:
當(dāng)然在運(yùn)行npm時(shí)是需要安裝npm install ewtree -S
,實(shí)現(xiàn)組件化
最終效果如下:
總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)樹形結(jié)構(gòu)樣式和功能的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Vue?elementUI實(shí)現(xiàn)樹形結(jié)構(gòu)表格與懶加載
- vue實(shí)現(xiàn)樹形結(jié)構(gòu)增刪改查的示例代碼
- vue開發(fā)樹形結(jié)構(gòu)組件(組件遞歸)
- vue Element-ui表格實(shí)現(xiàn)樹形結(jié)構(gòu)表格
- vue實(shí)現(xiàn)的樹形結(jié)構(gòu)加多選框示例
- vue樹形結(jié)構(gòu)獲取鍵值的方法示例
- Vue.js 遞歸組件實(shí)現(xiàn)樹形菜單(實(shí)例分享)
- vuejs使用遞歸組件實(shí)現(xiàn)樹形目錄的方法
- 用 Vue.js 遞歸組件實(shí)現(xiàn)可折疊的樹形菜單(demo)
- vue遞歸組件實(shí)現(xiàn)樹形結(jié)構(gòu)
相關(guān)文章
教你如何使用VUE組件創(chuàng)建SpreadJS自定義單元格
這篇文章主要介紹了使用VUE組件創(chuàng)建SpreadJS自定義單元格的方法,通常我們使用組件的方式是,在實(shí)例化Vue對(duì)象之前,通過Vue.component方法來注冊(cè)全局的組件,文中通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-01-01Vue3+Vite+TS實(shí)現(xiàn)二次封裝element-plus業(yè)務(wù)組件sfasga
這篇文章主要介紹了在Vue3+Vite+TS的基礎(chǔ)上實(shí)現(xiàn)二次封裝element-plus業(yè)務(wù)組件sfasga,下面文章也將圍繞實(shí)現(xiàn)二次封裝element-plus業(yè)務(wù)組件sfasga的相關(guān)介紹展開相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可惡意參考一下2021-12-12使用vue-router為每個(gè)路由配置各自的title
這篇文章主要介紹了如何使用vue-router為每個(gè)路由配置各自的title,及使用vue router的方法,需要的朋友可以參考下2018-07-07Vue.js 實(shí)現(xiàn)微信公眾號(hào)菜單編輯器功能(二)
這篇文章主要介紹了Vue.js 實(shí)現(xiàn)微信公眾號(hào)菜單編輯器功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05vue.prototype和vue.use的區(qū)別和注意點(diǎn)小結(jié)
這篇文章主要介紹了vue.prototype和vue.use的區(qū)別和注意點(diǎn)小結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04vue中使用gantt-elastic實(shí)現(xiàn)可拖拽甘特圖的示例代碼
這篇文章主要介紹了vue中使用gantt-elastic實(shí)現(xiàn)可拖拽甘特圖,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07vue語法自動(dòng)轉(zhuǎn)typescript(解放雙手)
這篇文章主要介紹了vue語法自動(dòng)轉(zhuǎn)typescript,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09解決vue中使用swiper插件問題及swiper在vue中的用法
Swiper是純javascript打造的滑動(dòng)特效插件,面向手機(jī)、平板電腦等移動(dòng)終端。這篇文章主要介紹了解決vue中使用swiper插件及swiper在vue中的用法,需要的朋友可以參考下2018-04-04