Vue ElementUI implements asynchronous loading tree

Vue ElementUI implements asynchronous loading tree

This article example shares the specific code of vue ElementUI to implement asynchronous loading tree for your reference. The specific content is as follows

Routing file modification

import List from '@/components/list.vue'
import Add from '@/components/add.vue'
import Tree from '@/components/tree.vue'
import AsynTree from '@/components/asyntree.vue'

export default{
    routes:[
        {path:"/list",component:List},
        {path:"/add",component:Add},
        {path:"/add/:id",component:Add},
        {path:"/tree",component:Tree},
        {path:"/asyntree",component:AsynTree}
    ]

}

Homepage app.vue

<template>
  <div id="app">
    <router-link to="/add">Add</router-link>&nbsp;&nbsp;
    <router-link to="/list">List</router-link>&nbsp;&nbsp;
    <router-link to="/tree">Tree structure</router-link>&nbsp;&nbsp;
    <router-link to="/asyntree">Asynchronous tree structure</router-link>&nbsp;&nbsp;
    <router-view></router-view>
  </div>
</template>

<script>
import List from './components/list.vue'

export default {
  name: 'app',
  components:
    List
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Asynchronously load the tree page

<template>


<el-container>
  <el-aside width="200px">
    <el-tree ref="tree"
    :data="data"
    lazy
    show-checkbox
    node-key="id"
    check-strictly
    :load="loadnode"
    :props="defaultProps"
    @node-click="nodeclick">
    </el-tree>
  </el-aside>
  <el-main>

    <el-form :model="typeinfo" class="demo-form-inline">
    <el-form-item label="ID">
        <el-input v-model="typeinfo.id" readonly></el-input>
    </el-form-item>
    <el-form-item label="Category Name">
        <el-input v-model="typeinfo.label" placeholder="category name"></el-input>
    </el-form-item>
    <el-form-item label="Serial number">
        <el-input v-model="typeinfo.seqno" placeholder="Serial number"></el-input>
    </el-form-item>
   <el-form-item label="Parent ID">
        <el-input v-model="typeinfo.pid" readonly></el-input>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" @click="dosave">Save</el-button>
        <el-button type="primary" @click="dochildsave">Add node</el-button>
    </el-form-item>
    </el-form>

  </el-main>
</el-container>

</template>

<script>
import axios from 'axios'

export default {
    data() {
        return {
            data:[], //Tree object data model defaultProps: { //Tree object property correspondence children: 'children',
                label: 'label'
            },
            typeinfo: {//Product classification entity object id:'',
                pid:'',
                label: '',
                seqno: ''
            }
        }
    },
    methods: {
        loadnode(node,resolve){
            //If you expand the first-level node, load the first-level node list from the background if (node.level==0)
            {
                this.loadfirstnode(resolve);
            }
            //If you expand other level nodes, dynamically load the next level node list from the background if (node.level>=1)
            {
                this.loadchildnode(node,resolve);
            }
        },
        //Load the first level node loadfirstnode(resolve){
            axios.get('http://localhost:6060/loadtype')
                .then(function(resp){
                    resolve(resp.data);
                })
        },
        //Refresh tree component refreshtree(){
            var _this = this;
            axios.get('http://localhost:6060/loadtype')
                .then(function(resp){
                    _this.data = resp.data;
                })
        },
        //Load the node's child node collection loadchildnode(node,resolve){
            axios.get('http://localhost:6060/loadtypechild?pid='+node.data.id)
                .then(function(resp){
                    resolve(resp.data);
                })
        },
        //Click the event triggered on the node, pass three parameters, the data object uses the first parameter nodeclick(data,dataObj,self)
        {
            //alert(data.label+",id="+data.id);
            this.typeinfo.id=data.id;
            this.typeinfo.pid=data.pid;
            this.typeinfo.label=data.label;
            this.typeinfo.seqno=data.seqno;
        },
        //Save classification method dosave()
        {
            var _this = this;
             axios.post('http://localhost:6060/savetype',this.typeinfo)
                .then(function(resp){
                    if(resp.data)
                        _this.refreshtree();
                })
        },
        //Save subclassification method dochildsave()
        {
            //Judge whether a node is selected in the left tree componentvar cnt=this.$refs['tree'].getCheckedNodes().length;
            if(cnt!=1)
            {
                this.$message('You must select a unique parent node');
                return;
            }
            //Get the tree object through this.$refs['tree'], where tree is the ref attribute of the tree component var dataObj = this.$refs['tree'].getCheckedNodes()[0];
    
            this.typeinfo.id='';
            this.typeinfo.pid=dataObj.id;
            var _this = this;
            axios.post('http://localhost:6060/savetype',this.typeinfo)
                .then(function(resp){
                    if(resp.data)
                        _this.refreshtree();
                })
        }
    }

}
</script>

Background Controller

@RequestMapping("/loadtype")
 @ResponseBody
 public List<TypeInfo> getTypeJson()
 {
  List<TypeInfo> rtn = getFirstNode();
  
  return rtn;
 }
  
 @RequestMapping("/loadtypechild")
 @ResponseBody
 public List<TypeInfo> getTypeByPid(Integer pid)
 {
  System.out.println("pid==="+pid);
  List<TypeInfo> rtn = addsrv.getTypeList(pid);
  
  return rtn;
 }
 
 private List<TypeInfo> getFirstNode()
 {
  TypeInfo root = addsrv.getRootType();
  List<TypeInfo> firstList = addsrv.getTypeList(root.getId());
  for(TypeInfo ti:firstList)
   recurseNode(ti);
  return firstList;
 }
 
 private void recurseNode(TypeInfo ti)
 {
  List<TypeInfo> children = addsrv.getTypeList(ti.getId());
  System.out.println("ti.id"+ti.getId()+",children="+children);
  if (children == null || children.size() == 0)
   return;
  ti.setChildren(children);
  for(TypeInfo chd:children)
  {
   recurseNode(chd);
  }
 }
 
 @RequestMapping("/savetype")
 @ResponseBody
 public Boolean savetype(@RequestBody TypeInfo ti)
 {
  try {
   Integer id = ti.getId();
   if(id != null)
    addsrv.updateType(ti);
   else {
    addsrv.saveType(ti);
   }
   return true;
  } catch (Exception e) {
   return false;
  }
  
 }

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:
  • Solve the problem that vue dynamic routing asynchronously loads import components and cannot load modules
  • Detailed explanation of the solution to the problem of asynchronously loading modules in Import of vue-router
  • Vue+echarts realizes the method of dynamically drawing charts and asynchronously loading data
  • Vue awesome swiper asynchronous loading data bug
  • Implementation of asynchronous loading of Amap by Vue
  • Detailed explanation of three usage examples of Vue+webpack for asynchronous loading
  • Vue asynchronously loads the about component
  • Detailed explanation of some points to note when loading data asynchronously in Vue-Cli
  • Javascript vue.js table paging, ajax asynchronous loading of data
  • Vue.js table paging ajax asynchronous loading of data

<<:  Detailed explanation of the index and storage structure of the MySQL InnoDB engine

>>:  Installation and configuration tutorial of Linux virtual machine under Windows operating system

Recommend

CSS eight eye-catching HOVER effect sample code

1. Send effect HTML <div id="send-btn&quo...

CocosCreator Typescript makes Tetris game

Table of contents 1. Introduction 2. Several key ...

How to use Lottie animation in React Native project

Lottie is an open source animation library for iO...

How to get the real path of the current script in Linux

1. Get the real path of the current script: #!/bi...

Small paging design

Let our users choose whether to move forward or ba...

js to achieve a simple magnifying glass effect

This article shares the specific code of js to ac...

Perfect solution to Google Chrome autofill problem

In Google Chrome, after successful login, Google ...

Detailed steps for setting up and configuring nis domain services on Centos8

Table of contents Introduction to NIS Network env...

How to implement paging query in MySQL

SQL paging query:background In the company's ...

Web Design Tutorial (6): Keep your passion for design

<br />Previous article: Web Design Tutorial ...

Detailed explanation of vue.js dynamic components

:is dynamic component Use v-bind:is="compone...

JavaScript flow control (branching)

Table of contents 1. Process Control 2. Sequentia...

How to start jar package and run it in the background in Linux

The Linux command to run the jar package is as fo...

How to move a red rectangle with the mouse in Linux character terminal

Everything is a file! UNIX has already said it. E...