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 Class Component Functional Comp...
Table of contents Start Docker Stop Docker Python...
1. I recently installed a new version of Ubuntu. ...
Table of contents Proxy forwarding rules The firs...
<br />How can I remove the scroll bar on the...
The detailed steps for installing mysql5.7.19 on ...
Method 1 Copy code The code is as follows: documen...
Panther started as a rookie, and I am still a roo...
Introduction to JWT What is JWT The full name is ...
Nginx first decides which server{} block in the c...
Introduction: When using MySQL to create a table,...
Table of contents 1. Preparation 1. Prepare the e...
version: centos==7.2 jdk==1.8 confluence==6.15.4 ...
After nginx is compiled and installed and used fo...
1. Install libfastcommon-1.0.43. The installation...