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:
|
<<: Introduction to MySQL triggers, creation of triggers and analysis of usage restrictions
>>: Nginx+FastDFS to build an image server
Table of contents Preface: 1. Introduction to Use...
Table of contents Workaround Why can't I moni...
Background <br />Students who work on the fr...
I've been a little busy these two days, and t...
I encountered a problem today. When entering the ...
need When querying a field, you need to give the ...
When I first came into contact with HTML, I alway...
Microsoft IIS IIS (Internet Information Server) i...
Note: nginx installed via brew Website root direc...
Table of contents 1. Scenario 2. Implement IJavaS...
background: I have done a project before, which r...
Table of contents 1. MHA 1. Concept 2. Compositio...
Table of contents 1. Lvs Introduction 2. Lvs load...
Preparation 1. Environmental Description: Operati...
Install axios and implement communication Here we...