Vue樹表格分頁的實(shí)現(xiàn)方法詳解
1. 準(zhǔn)備工作
- 創(chuàng)建測試數(shù)據(jù)庫
- 準(zhǔn)備好后臺服務(wù)接口,Moudel查詢,和Book查詢(支持分頁)
- 后臺單元測試
- 修改vue配置,使用真實(shí)環(huán)境

2. 動態(tài)樹
2.1 在配置請求路徑
在src/api/action.js中配置獲取動態(tài)樹數(shù)據(jù)的請求路徑
export default {
//服務(wù)器
'SERVER': 'http://localhost:8080/webserver',
//登陸請求
'SYSTEM_USER_DOLOGIN': '/userMsg/userAction!login.action', //登陸
//獲取動態(tài)樹數(shù)據(jù)請求
'SYSTEM_MODULE_REQ': '/sysMsg/sysMsgAction!getModules.action',
//獲取完整的請求地址
'getFullPath': k => { //獲得請求的完整地址,用于mockjs測試時(shí)使用
return this.SERVER + this[k];
}
}2.2 使用動態(tài)數(shù)據(jù)構(gòu)建導(dǎo)航菜單
2.2.1 通過接口獲取數(shù)據(jù)
LeftAside.vue:

//聲明周期鉤子函數(shù),此時(shí)的Vue實(shí)例已經(jīng)創(chuàng)建,且data和methods已經(jīng)創(chuàng)建,但沒有開始編譯模板
//利用該鉤子函數(shù)獲取動態(tài)樹數(shù)據(jù)
created: function() {
let url = this.axios.urls.SYSTEM_MODULE_REQ;
this.axios.get(url, {}).then(resp => {
//在data中聲明moduleDatas數(shù)組,接收返回的數(shù)據(jù),以便于在template中使用數(shù)據(jù)雙向綁定
this.moduleDatas = resp.data;
console.log(resp.data);
}).catch(resp => {});
//登錄成功后默認(rèn)顯示系統(tǒng)首頁
this.$router.push("/Home");
}測試,通過控制臺查看數(shù)據(jù)是否正常獲?。?/p>

2.2.2 通過后臺獲取的數(shù)據(jù)構(gòu)建菜單導(dǎo)航
先構(gòu)建一級導(dǎo)航菜單
LeftAside.vue:

<el-submenu v-for="(m1) in moduleDatas" :key="m1.id" :index="'index_'+m1.id">
<template slot="title">
<i :class="m1.icon"></i>
<span slot="title">{{m1.text}}</span>
</template>
</el-submenu>頁面效果:

構(gòu)建二級導(dǎo)航菜單
LeftAside.vue:

<!--
使用v-for生成二級導(dǎo)航菜單,index為功能url值,二級菜單為葉子節(jié)點(diǎn),為具體功能的功能菜單,
所以url一定有值(一級菜單的url為空)。
測試數(shù)據(jù)二級菜單沒有分組,所以不用el-menu-item-group,只要生成el-menu-item即可。
-->
<el-menu-item v-for="m2 in m1.childrens" :key="m2.id" :index="m2.url">
<span>{{m2.text}}</span>
</el-menu-item>頁面效果:

2.3 點(diǎn)擊菜單實(shí)現(xiàn)路由跳轉(zhuǎn)
2.3.1 創(chuàng)建書本管理組件
t_module_vue表中已經(jīng)配置了功能url,為方便,將書本管理組件定義為BookList。如果使用其他名字則需要修改功能url配置,保持一致。

2.3.2 配置路由

2.3.3 修改LeftAside組件

2.3.4 修改Main組件

3. 系統(tǒng)首頁配置
首先創(chuàng)建一個(gè)首頁組件

在Main組件中指定的<router-view/>是用于顯示各功能組件的。
配置路由:

配置首頁菜單:

菜單圖標(biāo)可以到官網(wǎng)去查找。
設(shè)置登錄成功后默認(rèn)顯示系統(tǒng)首頁:

<!--設(shè)置首頁菜單及其圖標(biāo),index設(shè)置的是Home組件的path-->
<el-menu-item key="home" index="/Home">
<i class="el-icon-s-home"></i>
<span>首頁</span>
</el-menu-item>4. 表格數(shù)據(jù)顯示
4.1 頁面布局
頁面上使用的面包屑,查詢條件,表格,分頁等空間,可以查看element-ui官網(wǎng)。該步驟主要關(guān)注頁面布局,并沒有綁定數(shù)據(jù),編寫完成后,觀察頁面效果。
BookList.vue:
<template>
<div style="margin-left: 15px; margin-right: 15px;">
<!--面包屑-->
<el-breadcrumb style="margin-top:15px;" separator="/">
<el-breadcrumb-item :to="{path: '/Home'}">首頁</el-breadcrumb-item>
<el-breadcrumb-item>書本管理</el-breadcrumb-item>
</el-breadcrumb>
<!--查詢條件-->
<el-form style="margin-top: 15px;" :inline="true" class="demo-form-inline">
<el-form-item label="書名">
<el-input placeholder="書名"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary">查詢</el-button>
<el-button type="primary">新增</el-button>
</el-form-item>
</el-form>
<!--表格-->
<el-table style="width: 100%;" :border="true" max-height="550">
<el-table-column prop="id" label="編號" min-width="40" align="center"></el-table-column>
<el-table-column prop="bookname" label="名稱" min-width="100" align="center"></el-table-column>
<el-table-column prop="price" label="價(jià)格" min-width="70" align="center"></el-table-column>
<el-table-column prop="booktype" label="類型" min-width="70" align="center"></el-table-column>
</el-table>
<!--分頁-->
<div class="block" style="text-align:right;margin-top:10px;">
<el-pagination
:page-sizes="[10, 20, 30, 40]"
:page-size="100"
layout="total, sizes, prev, pager, next, jumper"
:total="400">
</el-pagination>
</div>
</div>
</template>4.2 查詢并在表格中顯示數(shù)據(jù)
先不考慮分頁,從后臺接口獲取數(shù)據(jù)并綁定到表格顯示。
將查詢書本信息的接口配置到api/action.js中
//獲取書本信息 'BOOKMSG_BOOKINFO_REQ':'/bookMsg/bookAction!getBooks.action',
BookList.vue組件
圖一: template部分:

圖二: script部分

export default {
name: 'BookList',
data: function() {
return {
bookname: '',
books: []
}
},
methods: {
qry: function() {
let url = this.axios.urls.BOOKMSG_BOOKINFO_REQ;
this.axios.post(url, {
bookname: this.bookname
}).then(resp => {
console.log(resp);
this.books = resp.data.data;
}).catch(error => {
console.log(error);
});
}
}
}4.3 實(shí)現(xiàn)分頁
template部分:

<!--分頁-->
<div class="block" style="text-align:right;margin-top:10px;">
<!--
@size-chang: 定義在每頁顯示的記錄數(shù)變化時(shí)的處理函數(shù)
@current-change:當(dāng)前頁碼發(fā)生變化時(shí)的處理函數(shù),如點(diǎn)擊頁碼或輸入一個(gè)特定頁碼。
:current-page:指定當(dāng)前頁,
:page-size:每頁顯示的記錄數(shù)
layout: 布局,可以通過調(diào)整該項(xiàng)來調(diào)整顯示內(nèi)容
:total: 總記錄數(shù)
-->
<el-pagination background
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="page"
:page-sizes="[10, 20, 30, 40]"
:page-size="rows" layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
</div>script部分,圖一

qry: function() {
let url = this.axios.urls.BOOKMSG_BOOKINFO_REQ;
this.axios.post(url, {
bookname: this.bookname,
//分頁參數(shù)
page: this.page,
rows: this.rows
}).then(resp => {
console.log(resp);
this.books = resp.data.data;
//獲取總頁數(shù)
this.total = resp.data.total;
}).catch(error => {
console.log(error);
});
}script部分,圖二:

//當(dāng)每頁顯示的記錄數(shù)發(fā)生變化時(shí),設(shè)置當(dāng)前頁碼為1,執(zhí)行查詢。
handleSizeChange: function(rows) {
this.rows = rows;
this.page = 1;
this.qry();
},
//當(dāng)前頁碼發(fā)生變化時(shí),執(zhí)行查詢
handleCurrentChange: function(page) {
this.page = page;
this.qry();
}
到此這篇關(guān)于Vue樹表格分頁的實(shí)現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)Vue樹表格分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目中使用ts(typescript)入門教程
最近項(xiàng)目需要將原vue項(xiàng)目結(jié)合ts的使用進(jìn)行改造,本文從安裝到vue組件編寫進(jìn)行了說明,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
vue-print-nb實(shí)現(xiàn)頁面打印功能實(shí)例(含分頁打印)
在項(xiàng)目中,有時(shí)需要打印頁面的表格,在網(wǎng)上找了一個(gè)打印組件vue-print-nb,用了還不錯(cuò),所以下面這篇文章主要給大家介紹了關(guān)于vue-print-nb實(shí)現(xiàn)頁面打印功能的相關(guān)資料,需要的朋友可以參考下2022-08-08
vue-element-admin如何設(shè)置默認(rèn)語言
這篇文章主要介紹了vue-element-admin如何設(shè)置默認(rèn)語言,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue+element-ui表格自定義列模版的實(shí)現(xiàn)
本文主要介紹了vue+element-ui表格自定義列模版的實(shí)現(xiàn),通過插槽完美解決了element-ui表格自定義列模版的問題,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
淺析Vue3中Excel下載模板并導(dǎo)入數(shù)據(jù)功能的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Vue3中的Excel數(shù)據(jù)管理,即下載模板并導(dǎo)入數(shù)據(jù)功能的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2024-05-05
vue+elementUI實(shí)現(xiàn)點(diǎn)擊左右箭頭切換按鈕功能
這篇文章主要介紹了vue+elementUI實(shí)現(xiàn)點(diǎn)擊左右箭頭切換按鈕功能,樣式可以根據(jù)自己需求改動,感興趣的朋友可以參考下實(shí)現(xiàn)代碼2024-05-05
Vue2組件tree實(shí)現(xiàn)無限級樹形菜單
這篇文章主要為大家詳細(xì)介紹了Vue2組件tree實(shí)現(xiàn)無限級樹形菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

