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在antDesign框架或elementUI框架組件native事件中觸發(fā)2次問題
這篇文章主要介紹了vue在antDesign框架或elementUI框架組件native事件中觸發(fā)2次問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue實現(xiàn)分環(huán)境打包步驟(給不同的環(huán)境配置相對應(yīng)的打包命令)
這篇文章主要介紹了vue實現(xiàn)分環(huán)境打包步驟(給不同的環(huán)境配置相對應(yīng)的打包命令),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06