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

Implementation of MySQL scheduled database backup (full database backup)

Table of contents 1. MySQL data backup 1.1, mysql...

Detailed process of installing and deploying onlyoffice in docker

0. System requirements CPU I5-10400F or above Mem...

Detailed steps for building Portainer visual interface with Docker

In order to solve the problem mentioned last time...

Detailed installation process of MySQL5.6.40 under CentOS7 64

MySQL5.6.40 installation process under CentOS7 64...

Solution to MySql Error 1698 (28000)

1. Problem description: MysqlERROR1698 (28000) so...

Detailed explanation of keepAlive usage in Vue front-end development

Table of contents Preface keep-avlive hook functi...

How to use CSS custom variables in Vue

Table of contents The CSS custom variable functio...

my.cnf parameter configuration to optimize InnoDB engine performance

I have read countless my.cnf configurations on th...

How to implement the webpage anti-copying function (with cracking method)

By right-clicking the source file, the following c...

Native JS to achieve digital table special effects

This article shares a digital clock effect implem...

JS implementation of Apple calculator

This article example shares the specific code of ...