Vue Element-ui implements tree control node adding icon detailed explanation

Vue Element-ui implements tree control node adding icon detailed explanation

1. Rendering

Rendering

2. Bind data and add labels to the tree table

If you want to add an image or element-ui icon to the tree node of the tree control, you can add a label icon to the tree table binding data.

   children: [
       {
           icon:'el-icon-top-right',
           label: ['beam name',''],
           children: [
               {
                   label:['name','RS49'],
               },
               {
                   icon:'src/assets/images/Organization.png',
                   label:['group('+'3'+')','']
                   children:[
                   {
                   label:['10600361','10950','11200','0']
                    }
   					]
				}
           ]
		}
    ],

In the custom function of the tree control

Directly set the class to be equal to the icon tag of element-ui

The img tag needs to add the address of your own picture

    renderContent(h,{node,data,store}){
        //div represents a row of the tree control, and div contains three span tags // Determine the number of label arrays of the node and select the class through ternary operation
        // Set the class to control the alignment of the tree control return h('div',[
          // Add icon and image tags in the custom function of the tree control // The img tag needs to add the address of its own image h('span',{class:'top-right'}),
          h('img',{src:data.icon}),
          h('span', {class:node.label.length === 2 ? 'nodeStyle':'groupStyle'},node.label[0]),
          h('span', {class:'groupStyle'},node.label[1]),
          h('span',{class:node.label.length === 2 ? 'nodeStyle':'groupStyle'},node.label.length === 2 ? '':node.label[2])
          ]);
    },

3. All codes

<template>
    <div class="mytree">
          <el-tree
              :data="tree_data"
              :props="defaultProps"
              @node-click="handleNodeClick"
              indent="0"
              :render-content="renderContent"
          ></el-tree>
        </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
export default defineComponent({
    components: {},
    data() {
        return {
              tree_data: [
        		{
          // type:1,
         		 label: 'notice-id1',
                  children: [
                        {

                          label: ['Satellite name code', 'ZOHREH-2'],
                        },
                        {

                          label: ['Organization', 'IRN'],
                        },
                        {
                          label: ['Frequency range', '10950-1450'],
                        },
                        {
                          icon:'el-icon-top-right',
                          label: ['beam name',''],
                          children: [
                              {
                                  label:['name','RS49'],
                              },
                             {
                                  label:['freq_min','10950'],
                              },
                             {
                                  label:['freq_max','14500'],
                              },
                              {
                                  icon:'src/assets/images/Organization.png',
                                  label:['group('+'3'+')','']
                                  children:[
                                     {
                                        label:['10600361','10950','11200','0']
                                     },
                                    {
                                        label:['10600361','10950','11200','0']
                                     },
                                    {
                                        label:['10600361','10950','11200','0']
                                     }
                                  ]
                              }
                      ]
                    },
                  ],
                },
              ],
            defaultProps: {
            children: 'children',
            label: 'label',
          },
        }
    },
    method:{
    // Custom tree control function node represents each node renderContent(h,{node,data,store}){
        //div represents a row of the tree control, and div contains three span tags // Determine the number of label arrays of the node and select the class through ternary operation
        // Set the class to control the alignment of the tree control return h('div',[
          // Add icon and picture labels in the tree control custom function h('span',{class:[data.icon,data.icon==='el-icon-top-right'? 'top-right':'bottom-left']}),
          h('img',{src:data.icon === 'src/assets/images/Organization.png' ? data.icon:''}),
          h('span', {class:node.label.length === 2 ? 'nodeStyle':'groupStyle'},node.label[0]),
          h('span', {class:'groupStyle'},node.label[1]),
          h('span',{class:node.label.length === 2 ? 'nodeStyle':'groupStyle'},node.label.length === 2 ? '':node.label[2])
          ]);
    },
    }
    
})
</script>

<style lang="scss" scoped>
    
.nodeStyle{
  width:110px;
  display:inline-block;
  text-align:left;
}
.groupStyle{
  width:150px;
  display:inline-block;
  text-align:left;
}
    
</style>

Other Implementations

Vue implements tree table through element tree control

Add a dotted line to the element tree control

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Vue+ElementUI implements paging function-mysql data
  • Vue+elementui realizes multiple selection and search functions of drop-down table
  • Use vue2+elementui for hover prompts
  • Detailed explanation of the use of ElementUI in Vue
  • How to install Element UI and use vector graphics in vue3.0

<<:  Invalid solution when defining multiple class attributes in HTML

>>:  Writing High-Quality Code Web Front-End Development Practice Book Excerpts

Recommend

MySQL full-text search usage examples

Table of contents 1. Environmental Preparation 2....

VMware, nmap, burpsuite installation tutorial

Table of contents VMware BurpSuite 1. Virtual mac...

mysql-canal-rabbitmq installation and deployment super detailed tutorial

Table of contents 1.1. Enable MySQL binlog 1.2. C...

How to limit the input box to only input pure numbers in HTML

Limit input box to only pure numbers 1、onkeyup = ...

How to assign a public IP address to an instance in Linux

describe When calling this interface, you need to...

Docker Getting Started Installation Tutorial (Beginner Edition)

Doccer Introduction: Docker is a container-relate...

Advanced techniques for using CSS (used in actual combat)

1. The ul tag has a padding value by default in Mo...

Tomcat configuration and how to start it in Eclipse

Table of contents How to install and configure To...

How to remount the data disk after initializing the system disk in Linux

Remount the data disk after initializing the syst...

Detailed explanation of routes configuration of Vue-Router

Table of contents introduce Object attributes in ...

Native js realizes the drag and drop of the nine-square grid

Use native JS to write a nine-square grid to achi...