vue開發(fā)樹形結(jié)構(gòu)組件(組件遞歸)
本文實例為大家分享了vue開發(fā)樹形結(jié)構(gòu)組件的具體代碼,供大家參考,具體內(nèi)容如下
需求
一個頁面,要顯示商品分類,同時每個分類下面還擁有若干子類,子類也可以有子類。
要實現(xiàn)全選單選,子類被逐個全選父類也要標(biāo)記選中。
第一反應(yīng)就是樹形結(jié)構(gòu),和遞歸調(diào)用。曾經(jīng)在做WPF時記得有現(xiàn)成的組件,也學(xué)著寫過對應(yīng)的后臺。這次我要自己寫一個前端的組件了。
這只是我自己花了點時間寫的一個vue組件,尚可優(yōu)化及拓展。僅此與大家分享一下。
效果

實現(xiàn)
<template>
<div id="TreeMenu">
<div v-for="(node, index) in nodes" :class="{'TreeMenu-row-border-bottom': !depth}">
<div class="TreeMenu-row">
<img class="TreeMenu-row-selectimg" src="../assets/img/MembersPriceActivity/selected.png" @click="selectNode(0,node)" v-show="node.IsSelected"/>
<img class="TreeMenu-row-selectimg" src="../assets/img/MembersPriceActivity/select.png" @click="selectNode(1,node)" v-show="!node.IsSelected"/>
<div class="TreeMenu-row-firstdiv" :class="{'TreeMenu-row-border-bottom': node.ChildTypeList&&node.IsExpanded }" @click="expandNode(!node.IsExpanded,node)">
<label v-text="node.Name"></label>
<img class="TreeMenu-row-arrowimg" src="../assets/img/MembersPriceActivity/top.png" v-if="node.ChildTypeList" v-show="!node.IsExpanded">
<img class="TreeMenu-row-arrowimg" src="../assets/img/MembersPriceActivity/down.png" v-if="node.ChildTypeList" v-show="node.IsExpanded">
</div>
<TreeMenu :nodes="node.ChildTypeList" :fatherIndex="index" :depth="depth+1" v-on:selectFatherNode="selectFatherNode" v-if="node.ChildTypeList" v-show="!node.IsExpanded"></TreeMenu>
</div>
</div>
</div>
</template>js:
<script>
export default{
name: 'TreeMenu',
data () {
return {
goodstype: {
ID: '',
ParentID: '',
Name: '',
Code: '',
Level: 0,
ImgUrl: null,
ChildTypeList: []
}
}
},
props: {
nodes: {
type: Array,
default: () => {
return []
}
},
fatherIndex: {
type: Number,
default: 0
},
depth: {
type: Number,
default: 0
}
},
watch: {},
created () {},
mounted () {},
destroyed () {},
methods: {
// 選中/取消 當(dāng)前節(jié)點
selectNode (choice, node) {
node.IsSelected = choice
this.selectChildrenNode(choice, node.ChildTypeList || [])
this.$emit('selectFatherNode', choice, this.fatherIndex, this.nodes.every((node) => { return node.IsSelected === choice }))
},
// 子節(jié)點修改選中狀態(tài)
selectChildrenNode (choice, nodes, self) {
let _self = self || this
nodes.forEach((node) => { node.IsSelected = choice; _self.selectChildrenNode(choice, node.ChildTypeList || [], _self) })
},
// 作為父級節(jié)點檢查是否需要修改選中狀態(tài)(僅用于子節(jié)點調(diào)用)
selectFatherNode (choice, index, childrenState) {
if (choice) {
// 若其[Index]節(jié)點下子節(jié)點均為被選中狀態(tài),該[Index]節(jié)點就應(yīng)該被選中
if (childrenState) {
this.nodes[index].IsSelected = choice
this.$emit('selectFatherNode', choice, this.fatherIndex, this.nodes.every((node) => { return node.IsSelected === choice }))
}
} else {
// 若其[Index]節(jié)點下子節(jié)點有未被選中狀態(tài)的,該[Index]節(jié)點就應(yīng)該未選中
this.nodes[index].IsSelected = choice
this.$emit('selectFatherNode', choice, this.fatherIndex, false)
}
},
// 展開/收起 當(dāng)前節(jié)點
expandNode (choice, node) {
node.IsExpanded = choice
if (!choice) {
this.expandChildrenNode(choice, node.ChildTypeList)
}
},
// 子節(jié)點收起
expandChildrenNode (choice, nodes, self) {
let _self = self || this
nodes.forEach((node) => { node.IsExpanded = choice; _self.expandChildrenNode(choice, node.ChildTypeList || [], _self) })
}
}
}
</script>CSS:
<style lang="scss" scoped>
#TreeMenu {
.TreeMenu-row{
margin-left: 30px;
font-size: 15px;
padding: 12px 0 0 0;
}
.TreeMenu-row-firstdiv{
height: 32px;
margin-left: 30px;
}
.TreeMenu-row-arrowimg{
float: right;
margin-right: 15px;
width: 13px;
}
.TreeMenu-row-selectimg{
float: left;
width: 18px;
vertical-align: text-bottom;
}
.TreeMenu-row-border-bottom{
border-bottom: solid 1px #e6e6e6;
}
.TreeMenu-row-border-top{
border-top: solid 1px #e6e6e6;
}
}
</style>以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue?elementUI實現(xiàn)樹形結(jié)構(gòu)表格與懶加載
- vue實現(xiàn)樹形結(jié)構(gòu)增刪改查的示例代碼
- vue Element-ui表格實現(xiàn)樹形結(jié)構(gòu)表格
- vue實現(xiàn)樹形結(jié)構(gòu)樣式和功能的實例代碼
- vue實現(xiàn)的樹形結(jié)構(gòu)加多選框示例
- vue樹形結(jié)構(gòu)獲取鍵值的方法示例
- Vue.js 遞歸組件實現(xiàn)樹形菜單(實例分享)
- vuejs使用遞歸組件實現(xiàn)樹形目錄的方法
- 用 Vue.js 遞歸組件實現(xiàn)可折疊的樹形菜單(demo)
- vue遞歸組件實現(xiàn)樹形結(jié)構(gòu)
相關(guān)文章
vue實現(xiàn)下拉框二級聯(lián)動效果的實例代碼
這篇文章主要介紹了vue實現(xiàn)下拉框二級聯(lián)動效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11
vue中回調(diào)函數(shù)(callback)的用法舉例
這篇文章主要給大家介紹了關(guān)于vue中回調(diào)函數(shù)(callback)的用法舉例,所謂的回調(diào)函數(shù),就是由調(diào)用函數(shù)提供執(zhí)行代碼,被調(diào)用函數(shù)執(zhí)行完畢之后,再自動執(zhí)行的一個函數(shù),需要的朋友可以參考下2023-08-08
vue組件傳值的實現(xiàn)方式小結(jié)【三種方式】
這篇文章主要介紹了vue組件傳值的實現(xiàn)方式,結(jié)合實例形式總結(jié)分析了vue.js組建傳值的三種實現(xiàn)方式,包括父傳子、子傳父及非父子傳值,需要的朋友可以參考下2020-02-02
解決vue elementUI 使用el-select 時 change事件的觸發(fā)問題
這篇文章主要介紹了解決vue elementUI 使用el-select 時 change事件的觸發(fā)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue項目中使用jsonp抓取跨域數(shù)據(jù)的方法
這篇文章主要介紹了Vue項目中使用jsonp抓取跨域數(shù)據(jù)的方法,本文通過實例代碼講解的非常詳細,需要的朋友可以參考下2019-11-11

