Vue development tree structure components (component recursion)

Vue development tree structure components (component recursion)

This article example shares the specific code of Vue development tree structure components for your reference. The specific content is as follows

need

A 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.

Effect

accomplish

<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:
  • Vue.js recursive component to implement tree menu (example sharing)
  • Vue.js component tree implements unlimited level tree menu
  • Vuejs uses recursive components to implement tree directories
  • Vue component tree implements tree menu
  • Vue2 component tree implements unlimited level tree menu
  • Sample code based on Vue's tree selection component
  • Using Vue.js recursive components to implement a collapsible tree menu (demo)
  • Vue component template form to realize the object array data loop into a tree structure (example code)
  • Vue2 recursive component to implement tree menu
  • Vue.js recursive component to build a tree menu

<<:  Solution to the conflict between Linux kernel and SVN versions

>>:  MySQL optimization tips: analysis of duplicate removal implementation methods [millions of data]

Recommend

Detailed explanation of the use of state in React's three major attributes

Table of contents Class Component Functional Comp...

Use Shell scripts to batch start and stop Docker services

Table of contents Start Docker Stop Docker Python...

Detailed explanation of various usages of proxy_pass in nginx

Table of contents Proxy forwarding rules The firs...

Useful codes for web page creation

<br />How can I remove the scroll bar on the...

How to install and configure mysql 5.7.19 under centos6.5

The detailed steps for installing mysql5.7.19 on ...

3 different ways to clear the option options in the select tag

Method 1 Copy code The code is as follows: documen...

CentOS 7.2 builds nginx web server to deploy uniapp project

Panther started as a rookie, and I am still a roo...

Node uses koa2 to implement a simple JWT authentication method

Introduction to JWT What is JWT The full name is ...

Nginx handles http request implementation process analysis

Nginx first decides which server{} block in the c...

What you need to know about MySQL auto-increment ID

Introduction: When using MySQL to create a table,...

Detailed process of using vmware to test PXE batch installation server

Table of contents 1. Preparation 1. Prepare the e...

How to deploy Confluence and jira-software in Docker

version: centos==7.2 jdk==1.8 confluence==6.15.4 ...

How to smoothly upgrade nginx after compiling and installing nginx

After nginx is compiled and installed and used fo...