vue ElementUI實(shí)現(xiàn)異步加載樹(shù)
本文實(shí)例為大家分享了vue ElementUI實(shí)現(xiàn)異步加載樹(shù)的具體代碼,供大家參考,具體內(nèi)容如下
路由文件修改
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} ] }
首頁(yè)app.vue
<template> <div id="app"> <router-link to="/add">添加</router-link> <router-link to="/list">列表</router-link> <router-link to="/tree">樹(shù)結(jié)構(gòu)</router-link> <router-link to="/asyntree">異步樹(shù)結(jié)構(gòu)</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>
異步加載樹(shù)頁(yè)面
<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="分類(lèi)名稱(chēng)"> <el-input v-model="typeinfo.label" placeholder="分類(lèi)名稱(chēng)"></el-input> </el-form-item> <el-form-item label="序號(hào)"> <el-input v-model="typeinfo.seqno" placeholder="序號(hào)"></el-input> </el-form-item> <el-form-item label="父ID"> <el-input v-model="typeinfo.pid" readonly></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="dosave">保存</el-button> <el-button type="primary" @click="dochildsave">添加節(jié)點(diǎn)</el-button> </el-form-item> </el-form> </el-main> </el-container> </template> <script> import axios from 'axios' export default { data() { return { data:[],//樹(shù)對(duì)象數(shù)據(jù)模型 defaultProps: {//樹(shù)對(duì)象屬性對(duì)應(yīng)關(guān)系 children: 'children', label: 'label' }, typeinfo: {//商品分類(lèi)實(shí)體對(duì)象 id:'', pid:'', label: '', seqno: '' } } }, methods: { loadnode(node,resolve){ //如果展開(kāi)第一級(jí)節(jié)點(diǎn),從后臺(tái)加載一級(jí)節(jié)點(diǎn)列表 if(node.level==0) { this.loadfirstnode(resolve); } //如果展開(kāi)其他級(jí)節(jié)點(diǎn),動(dòng)態(tài)從后臺(tái)加載下一級(jí)節(jié)點(diǎn)列表 if(node.level>=1) { this.loadchildnode(node,resolve); } }, //加載第一級(jí)節(jié)點(diǎn) loadfirstnode(resolve){ axios.get('http://localhost:6060/loadtype') .then(function(resp){ resolve(resp.data); }) }, //刷新樹(shù)組件 refreshtree(){ var _this = this; axios.get('http://localhost:6060/loadtype') .then(function(resp){ _this.data = resp.data; }) }, //加載節(jié)點(diǎn)的子節(jié)點(diǎn)集合 loadchildnode(node,resolve){ axios.get('http://localhost:6060/loadtypechild?pid='+node.data.id) .then(function(resp){ resolve(resp.data); }) }, //點(diǎn)擊節(jié)點(diǎn)上觸發(fā)的事件,傳遞三個(gè)參數(shù),數(shù)據(jù)對(duì)象使用第一個(gè)參數(shù) 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; }, //保存分類(lèi)方法 dosave() { var _this = this; axios.post('http://localhost:6060/savetype',this.typeinfo) .then(function(resp){ if(resp.data) _this.refreshtree(); }) }, //保存子分類(lèi)方法 dochildsave() { //判斷左側(cè)樹(shù)組件是否選擇了一個(gè)節(jié)點(diǎn) var cnt=this.$refs['tree'].getCheckedNodes().length; if(cnt!=1) { this.$message('必須選擇唯一父節(jié)點(diǎn)'); return; } //通過(guò)this.$refs['tree']獲取樹(shù)對(duì)象,其中tree是樹(shù)組件的ref屬性 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>
后臺(tái)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; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解vue-cli中的ESlint配置文件eslintrc.js
本篇文章主要介紹了vue-cli中的ESlint配置文件eslintrc.js詳解 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09手寫(xiě)實(shí)現(xiàn)vue2下拉菜單dropdown組件實(shí)例
這篇文章主要為大家介紹了手寫(xiě)vue2下拉菜單dropdown組件實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08在 vue-cli v3.0 中使用 SCSS/SASS的方法
關(guān)于如何在 vue-cli v3.0 中使用 SCSS/SASS,這里提供三種方案。感興趣的朋友通過(guò)本文一起學(xué)習(xí)吧2018-06-06vue.js中Vue-router 2.0基礎(chǔ)實(shí)踐教程
這篇文章主要給大家介紹了關(guān)于vue.js中Vue-router 2.0基礎(chǔ)實(shí)踐的相關(guān)資料,其中包括vue-router 2.0的基礎(chǔ)用法、動(dòng)態(tài)路由匹配、嵌套路由、編程式路由、命名路由以及命名視圖等相關(guān)知識(shí),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-05-05vue3中的reactive、readonly和shallowReactive使用詳解
在 Vue3 中,可以使用 shallowReactive 函數(shù)創(chuàng)建一個(gè)淺層響應(yīng)式對(duì)象,它接收一個(gè)普通對(duì)象作為參數(shù),返回一個(gè)淺層響應(yīng)式代理對(duì)象,本文給大家介紹vue3中的reactive、readonly和shallowReactive使用,感興趣的朋友跟隨小編一起看看吧2024-04-04vue如何實(shí)現(xiàn)級(jí)聯(lián)選擇器功能
這篇文章主要介紹了vue如何實(shí)現(xiàn)級(jí)聯(lián)選擇器功能問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04