欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue ElementUI實現(xiàn)異步加載樹

 更新時間:2021年06月06日 13:06:59   作者:陳岐祥  
這篇文章主要為大家詳細介紹了vue ElementUI實現(xiàn)異步加載樹,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue ElementUI實現(xiàn)異步加載樹的具體代碼,供大家參考,具體內容如下

路由文件修改

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}
    ]

}

首頁app.vue

<template>
  <div id="app">
    <router-link to="/add">添加</router-link>&nbsp;&nbsp;
    <router-link to="/list">列表</router-link>&nbsp;&nbsp;
    <router-link to="/tree">樹結構</router-link>&nbsp;&nbsp;
    <router-link to="/asyntree">異步樹結構</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>

異步加載樹頁面

<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="分類名稱">
        <el-input v-model="typeinfo.label" placeholder="分類名稱"></el-input>
    </el-form-item>
    <el-form-item label="序號">
        <el-input v-model="typeinfo.seqno" placeholder="序號"></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é)點</el-button>
    </el-form-item>
    </el-form>

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

</template>

<script>
import axios from 'axios'

export default {
    data() {
        return {
            data:[],//樹對象數(shù)據(jù)模型
            defaultProps: {//樹對象屬性對應關系
                children: 'children',
                label: 'label'
            },
            typeinfo: {//商品分類實體對象
                id:'',
                pid:'',
                label: '',
                seqno: ''
            }
        }
    },
    methods: {
        loadnode(node,resolve){
            //如果展開第一級節(jié)點,從后臺加載一級節(jié)點列表
            if(node.level==0)
            {
                this.loadfirstnode(resolve);
            }
            //如果展開其他級節(jié)點,動態(tài)從后臺加載下一級節(jié)點列表
            if(node.level>=1)
            {
                this.loadchildnode(node,resolve);
            }
        },
        //加載第一級節(jié)點
        loadfirstnode(resolve){
            axios.get('http://localhost:6060/loadtype')
                .then(function(resp){
                    resolve(resp.data);
                })
        },
        //刷新樹組件
        refreshtree(){
            var _this = this;
            axios.get('http://localhost:6060/loadtype')
                .then(function(resp){
                    _this.data = resp.data;
                })
        },
        //加載節(jié)點的子節(jié)點集合
        loadchildnode(node,resolve){
            axios.get('http://localhost:6060/loadtypechild?pid='+node.data.id)
                .then(function(resp){
                    resolve(resp.data);
                })
        },
        //點擊節(jié)點上觸發(fā)的事件,傳遞三個參數(shù),數(shù)據(jù)對象使用第一個參數(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;
        },
        //保存分類方法
        dosave()
        {
            var _this = this;
             axios.post('http://localhost:6060/savetype',this.typeinfo)
                .then(function(resp){
                    if(resp.data)
                        _this.refreshtree();
                })
        },
        //保存子分類方法
        dochildsave()
        {
            //判斷左側樹組件是否選擇了一個節(jié)點
            var cnt=this.$refs['tree'].getCheckedNodes().length;
            if(cnt!=1)
            {
                this.$message('必須選擇唯一父節(jié)點');
                return;
            }
            //通過this.$refs['tree']獲取樹對象,其中tree是樹組件的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>

后臺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;
  }
  
 }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 詳解vue-cli中的ESlint配置文件eslintrc.js

    詳解vue-cli中的ESlint配置文件eslintrc.js

    本篇文章主要介紹了vue-cli中的ESlint配置文件eslintrc.js詳解 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 手寫實現(xiàn)vue2下拉菜單dropdown組件實例

    手寫實現(xiàn)vue2下拉菜單dropdown組件實例

    這篇文章主要為大家介紹了手寫vue2下拉菜單dropdown組件實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 在 vue-cli v3.0 中使用 SCSS/SASS的方法

    在 vue-cli v3.0 中使用 SCSS/SASS的方法

    關于如何在 vue-cli v3.0 中使用 SCSS/SASS,這里提供三種方案。感興趣的朋友通過本文一起學習吧
    2018-06-06
  • vue中使用定義好的變量設置css樣式詳解

    vue中使用定義好的變量設置css樣式詳解

    vue項目中我們可以通過行內樣式進行動態(tài)修改樣式,下面這篇文章主要給大家介紹了關于vue中如何使用定義好的變量設置css樣式的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-03-03
  • vue.js中Vue-router 2.0基礎實踐教程

    vue.js中Vue-router 2.0基礎實踐教程

    這篇文章主要給大家介紹了關于vue.js中Vue-router 2.0基礎實踐的相關資料,其中包括vue-router 2.0的基礎用法、動態(tài)路由匹配、嵌套路由、編程式路由、命名路由以及命名視圖等相關知識,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • Vue3如何獲取來源路由

    Vue3如何獲取來源路由

    這篇文章主要介紹了Vue3如何獲取來源路由問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 詳解Vue組件之作用域插槽

    詳解Vue組件之作用域插槽

    這篇文章主要介紹了Vue組件之作用域插槽,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • vue3中的reactive、readonly和shallowReactive使用詳解

    vue3中的reactive、readonly和shallowReactive使用詳解

    在 Vue3 中,可以使用 shallowReactive 函數(shù)創(chuàng)建一個淺層響應式對象,它接收一個普通對象作為參數(shù),返回一個淺層響應式代理對象,本文給大家介紹vue3中的reactive、readonly和shallowReactive使用,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • vue如何實現(xiàn)級聯(lián)選擇器功能

    vue如何實現(xiàn)級聯(lián)選擇器功能

    這篇文章主要介紹了vue如何實現(xiàn)級聯(lián)選擇器功能問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 淺談vuex為什么不建議在action中修改state

    淺談vuex為什么不建議在action中修改state

    這篇文章主要介紹了淺談vuex為什么不建議在action中修改state,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02

最新評論