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

JavaScript Basics: Immediate Execution Function

Table of contents Immediately execute function fo...

MySQL 8.0.22.0 download, installation and configuration method graphic tutorial

MySQL 8.0.22 download, installation and configura...

Solution to the problem of null column in NOT IN filling pit in MySQL

Some time ago, when I was working on a small func...

Install nvidia graphics driver under Ubuntu (simple installation method)

Install the nvidia graphics card driver under Ubu...

Detailed explanation of the problem of CSS class names

The following CSS class names starting with a num...

Reasons and solutions for not being able to detect array changes in Vue2

Table of contents Workaround Why can't I moni...

Introduction to /etc/my.cnf parameters in MySQL 5.7

Below are some common parameters of /etc/my.cnf o...

The difference between mysql outer join and inner join query

The syntax for an outer join is as follows: SELEC...

Implementing WeChat tap animation effect based on CSS3 animation attribute

Seeing the recent popular WeChat tap function, I ...

Vue directives v-html and v-text

Table of contents 1. v-text text rendering instru...

MySQL database operation and maintenance data recovery method

The previous three articles introduced common bac...