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

Border-radius IE8 compatible processing method

According to canisue (http://caniuse.com/#search=...

js uses Canvas to merge multiple pictures into one implementation code

Solution function mergeImgs(list) { const imgDom ...

HTML simple web form creation example introduction

<input> is used to collect user information ...

Angular framework detailed explanation of view abstract definition

Preface As a front-end framework designed "f...

How to create a Docker repository using Nexus

The warehouse created using the official Docker R...

Difference between querySelector and getElementById methods in JS

Table of contents 1. Overview 1.1 Usage of queryS...

How to stop CSS animation midway and maintain the posture

Preface I once encountered a difficult problem. I...

Reasons why MySQL queries are slow

Table of contents 1. Where is the slowness? 2. Ha...

How to generate Vue user interface by dragging and dropping

Table of contents Preface 1. Technical Principle ...

Detailed explanation of CocosCreator MVC architecture

Overview This article will introduce the MVC arch...

vue+ts realizes the effect of element mouse drag

This article example shares the specific code of ...