vue開發(fā)樹形結(jié)構(gòu)組件(組件遞歸)
本文實(shí)例為大家分享了vue開發(fā)樹形結(jié)構(gòu)組件的具體代碼,供大家參考,具體內(nèi)容如下
需求
一個(gè)頁(yè)面,要顯示商品分類,同時(shí)每個(gè)分類下面還擁有若干子類,子類也可以有子類。
要實(shí)現(xiàn)全選單選,子類被逐個(gè)全選父類也要標(biāo)記選中。
第一反應(yīng)就是樹形結(jié)構(gòu),和遞歸調(diào)用。曾經(jīng)在做WPF時(shí)記得有現(xiàn)成的組件,也學(xué)著寫過對(duì)應(yīng)的后臺(tái)。這次我要自己寫一個(gè)前端的組件了。
這只是我自己花了點(diǎn)時(shí)間寫的一個(gè)vue組件,尚可優(yōu)化及拓展。僅此與大家分享一下。
效果
實(shí)現(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é)點(diǎn) 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é)點(diǎn)修改選中狀態(tài) selectChildrenNode (choice, nodes, self) { let _self = self || this nodes.forEach((node) => { node.IsSelected = choice; _self.selectChildrenNode(choice, node.ChildTypeList || [], _self) }) }, // 作為父級(jí)節(jié)點(diǎn)檢查是否需要修改選中狀態(tài)(僅用于子節(jié)點(diǎn)調(diào)用) selectFatherNode (choice, index, childrenState) { if (choice) { // 若其[Index]節(jié)點(diǎn)下子節(jié)點(diǎn)均為被選中狀態(tài),該[Index]節(jié)點(diǎn)就應(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é)點(diǎn)下子節(jié)點(diǎn)有未被選中狀態(tài)的,該[Index]節(jié)點(diǎn)就應(yīng)該未選中 this.nodes[index].IsSelected = choice this.$emit('selectFatherNode', choice, this.fatherIndex, false) } }, // 展開/收起 當(dāng)前節(jié)點(diǎn) expandNode (choice, node) { node.IsExpanded = choice if (!choice) { this.expandChildrenNode(choice, node.ChildTypeList) } }, // 子節(jié)點(diǎn)收起 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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue?elementUI實(shí)現(xiàn)樹形結(jié)構(gòu)表格與懶加載
- vue實(shí)現(xiàn)樹形結(jié)構(gòu)增刪改查的示例代碼
- vue Element-ui表格實(shí)現(xiàn)樹形結(jié)構(gòu)表格
- vue實(shí)現(xiàn)樹形結(jié)構(gòu)樣式和功能的實(shí)例代碼
- 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+element實(shí)現(xiàn)錨點(diǎn)鏈接方式
這篇文章主要介紹了vue+element實(shí)現(xiàn)錨點(diǎn)鏈接方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07vue在antDesign框架或elementUI框架組件native事件中觸發(fā)2次問題
這篇文章主要介紹了vue在antDesign框架或elementUI框架組件native事件中觸發(fā)2次問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue實(shí)現(xiàn)分環(huán)境打包步驟(給不同的環(huán)境配置相對(duì)應(yīng)的打包命令)
這篇文章主要介紹了vue實(shí)現(xiàn)分環(huán)境打包步驟(給不同的環(huán)境配置相對(duì)應(yīng)的打包命令),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06解決node-sass安裝報(bào)錯(cuò)無python等情況
在國(guó)內(nèi)安裝node-sass常因無法穩(wěn)定連接GitHub而失敗,解決方法包括手動(dòng)下載對(duì)應(yīng)的binding.node文件并放入緩存目錄,操作步驟詳細(xì),適合非Python用戶,無需額外環(huán)境配置2024-10-10