This article example shares the specific code of Vue development tree structure components for your reference. The specific content is as follows needA page should display product categories, and each category should have several subcategories, and subcategories can also have subcategories. To achieve single selection of all, the subclasses are selected one by one and the parent class must also be marked as selected. The first reaction is the tree structure and recursive calls. I remember that when I was working on WPF, there were ready-made components, and I also learned to write the corresponding background. This time I'm going to write a front-end component myself. This is just a vue component that I spent some time writing, which can be optimized and expanded. Just sharing this with you all. Effectaccomplish<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: { // Select/cancel the current node 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 })) }, // Change the selected state of child nodes selectChildrenNode (choice, nodes, self) { let _self = self || this nodes.forEach((node) => { node.IsSelected = choice; _self.selectChildrenNode(choice, node.ChildTypeList || [], _self) }) }, // Check whether the selected state needs to be modified as a parent node (only for child node calls) selectFatherNode (choice, index, childrenState) { if (choice) { // If all child nodes under the [Index] node are selected, the [Index] node should be selected if (childrenState) { this.nodes[index].IsSelected = choice this.$emit('selectFatherNode', choice, this.fatherIndex, this.nodes.every((node) => { return node.IsSelected === choice })) } } else { // If any child node under the [Index] node is not selected, the [Index] node should be unselected this.nodes[index].IsSelected = choice this.$emit('selectFatherNode', choice, this.fatherIndex, false) } }, // Expand/collapse the current node expandNode (choice, node) { node.IsExpanded = choice if (!choice) { this.expandChildrenNode(choice, node.ChildTypeList) } }, // collapse child nodes 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> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Solution to the conflict between Linux kernel and SVN versions
>>: MySQL optimization tips: analysis of duplicate removal implementation methods [millions of data]
Table of contents Immediately execute function fo...
MySQL 8.0.22 download, installation and configura...
Some time ago, when I was working on a small func...
Install the nvidia graphics card driver under Ubu...
The following CSS class names starting with a num...
Format Encoding 1. Please set the page width with...
Table of contents Workaround Why can't I moni...
Below are some common parameters of /etc/my.cnf o...
1. Overview I searched a lot online and found tha...
The syntax for an outer join is as follows: SELEC...
Seeing the recent popular WeChat tap function, I ...
Table of contents 1. v-text text rendering instru...
The previous three articles introduced common bac...
Table of contents Preface Implementation ideas Im...
Overview Let's summarize some SQL statements ...