Detailed explanation of using Vue custom tree control

Detailed explanation of using Vue custom tree control

This article shares with you how to use the Vue custom tree control for your reference. The specific content is as follows

Effect picture:

Data structure:

tree: {
        title: '', // title (name) 
        key: '0',
        head: '', // avatar selectStatus: false, // checkBox selected status children: [
          {
            title: 'Wangwang Episode 1',
            key: '0-0',
            head: '',
            selectStatus: false,
            children: [
              {
                key: '0-0-0',
                title: 'Wangzai 1',
                head: require('@/assets/wan.jpg'),
                selectStatus: false
              }
            ]
          },
          {
            title: 'Wangwang Part 2',
            key: '0-1',
            head: '',
            selectStatus: false,
            children: [
              {
                title: 'Wangwang Second Division First Team',
                key: '0-1-0',
                head: '',
                selectStatus: false,
                children: [
                  {
                    title: 'Wangwang Second Division, Team 1, Class 1',
                    key: '0-1-0-2',
                    head: '',
                    selectStatus: false,
                    children: [
                      {
                        title: 'Wang Zai 3',
                        key: '0-1-0-2-0',
                        head: require('@/assets/wan.jpg'),
                        selectStatus: false
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
},

Ideas:

/*The core of the custom tree control is "the component calls itself". Here, the tree control is encapsulated into a subcomponent*/
<template>
  <div>
    <div class="tree-custom">
      <div :style="indent" @click="toggleChildren"> //The toggleChildren event is a control event for "expand content" and "close content"/* 
       Here is the specific content of the recursive data display. For example, the specific content of the recursion in this project is "Picture/Avatar", "Title/Name", "null/CheckBox" from the effect diagram.
       The effect diagram shows the logic:
       <div v-if="!headImg && label" >
       //If no avatar image has a title, display the "arrow-title" style</div>
        <div v-if="headImg">
       //If there is an avatar picture, display the "avatar-name-checkBox" style</div>
       */      
      </div>
      <tree-custom // "call itself"
        :key="children.key" // key value is unique v-for="children in treeData"  
        v-if="showChildren" // Determine whether to expand the content based on the toggleChildren event:treeData="children.children" // The following are some properties, you should be able to understand them! No more words!
        :label="children.title"
        :headImg="children.head"
        :pkid="children.key"
        :depth="depth+1" // This is used to control the indentation style of each line. You can move to the bottom => indent () to see the specific usage: selectStatus="children.selectStatus"
        v-bind="$attrs" // These two are used to implement communication between grandparent and grandchild components v-on="$listeners"
      >
      </tree-custom>
    </div>
  </div>
</template>
<script>
export default {
  name: 'TreeCustom', // Give our component a name! Otherwise how to call data () {
    return {
      showChildren: true, // This is the data that controls whether to display the content~that is, expand and collapse!
      currentInfoData: {} // This is used to get the data of the current row. For the sake of simplicity, the specific use of the code above has been deleted by me ~ it is not very meaningful}
  },
  //The default value of the object should be returned by a factory function to avoid pitfalls props: {
    treeData: {
      type: Array,
      default: () => []
    },
    label: {
      type: String,
      default: () => ''
    },
    depth:
      type: Number,
      default: () => 0
    },
    headImg: {
      type: String,
      default: () => ''
    },
    pkid:
      type: String,
      default: () => ''
    },
    selectStatus: {
      type: Boolean,
      default: () => null
    }
  },
  computed: {
    indent () { // Define different levels of indentation styles return { transform: `translate(${(this.depth - 1) * 15}px)` }
    }
  },
  methods: {
    toggleChildren () {
      this.showChildren = !this.showChildren
    },
    checkBoxSelectChange (e) {
      const checked = e.target.checked
      if (checked) {
       //Use the $listeners method to call the ancestor's function. Because this is a recursive component, the components may not have a strict parent-child relationship, so methods such as $emit and $parent are not appropriate this.$listeners.addSelectedData(this.currentInfoData)
      }
      if (!checked) {
        this.$listeners.deleteSelectedData(this.currentInfoData)
      }
    },
    getCurrentInfo (label, headImg, pkid) {
      this.currentInfoData = {
        key: pkid,
        title: label,
        head:headImg
      }
    }
  }
}
</script>
/*Component calling method*/
<div class="tree-scroll">
  <tree-custom
    :label="tree.title"
    :headImg="tree.head"
    :treeData="tree.children"
    :pkid="tree.key"
    :depth="0"
    :selectStatus="tree.selectStatus"
    @addSelectedData="addSelectedData"
    @deleteSelectedData="deleteSelectedData" />
</div>

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:
  • Specific use of Vue+iview using tree controls
  • Solve the problem that the defaultExpandAll setting of the tree control in ant design vue is invalid
  • A case study of implementing Tree tree controls using Vue without relying on any third party
  • Vue+Element UI tree control integrates drop-down function menu (tree + dropdown +input)
  • Vue recursive component actual combat simple tree control example code
  • Detailed explanation of the vue-element Tree tree control filling pit
  • Vue elementUI tree tree control gets the parent node ID instance
  • Vue uses recursive components to write example code for tree controls
  • How to change vue.js element-ui tree tree control to iview
  • Sample code for adding add, delete and modify functions to VUE's Ele.me tree control

<<:  Introduction to MySQL triggers, creation of triggers and analysis of usage restrictions

>>:  Nginx+FastDFS to build an image server

Recommend

Detailed explanation of MySQL user rights management

Table of contents Preface: 1. Introduction to Use...

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

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

Detailed description of component-based front-end development process

Background <br />Students who work on the fr...

Examples of using temporary tables in MySQL

I've been a little busy these two days, and t...

Solve the problem of case sensitivity of Linux+Apache server URL

I encountered a problem today. When entering the ...

How to assign default values ​​to fields when querying MySQL

need When querying a field, you need to give the ...

Basic notes on html and css (must read for front-end)

When I first came into contact with HTML, I alway...

Steps to configure IIS10 under Win10 and support debugging ASP programs

Microsoft IIS IIS (Internet Information Server) i...

Implementation of multi-site configuration of Nginx on Mac M1

Note: nginx installed via brew Website root direc...

WebWorker encapsulates JavaScript sandbox details

Table of contents 1. Scenario 2. Implement IJavaS...

MySQL high availability cluster deployment and failover implementation

Table of contents 1. MHA 1. Concept 2. Compositio...

An example of using Lvs+Nginx cluster to build a high-concurrency architecture

Table of contents 1. Lvs Introduction 2. Lvs load...

Windows Server 2019 IIS10.0+PHP(FastCGI)+MySQL Environment Construction Tutorial

Preparation 1. Environmental Description: Operati...

Implementation of communication between Vue and Flask

Install axios and implement communication Here we...