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> <router-link to="/list">List</router-link> <router-link to="/tree">Tree structure</router-link> <router-link to="/asyntree">Asynchronous tree structure</router-link> <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:
|
<<: 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
Docker Compose Introduction to Compose Compose is...
Table of contents 1. MySQL data backup 1.1, mysql...
Preface This article mainly shares with you an ex...
0. System requirements CPU I5-10400F or above Mem...
In order to solve the problem mentioned last time...
1. Basic syntax structure of HTML submit and bott...
MySQL5.6.40 installation process under CentOS7 64...
1. Problem description: MysqlERROR1698 (28000) so...
Table of contents Preface keep-avlive hook functi...
1. Download, install and configure mysql-8.0.15 1...
Table of contents The CSS custom variable functio...
I have read countless my.cnf configurations on th...
By right-clicking the source file, the following c...
This article shares a digital clock effect implem...
This article example shares the specific code of ...