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

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

//聲明周期鉤子函數(shù),此時(shí)的Vue實(shí)例已經(jīng)創(chuàng)建,且data和methods已經(jīng)創(chuàng)建,但沒(méi)有開(kāi)始編譯模板
//利用該鉤子函數(shù)獲取動(dòng)態(tài)樹(shù)數(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)首頁(yè)
this.$router.push("/Home");
}測(cè)試,通過(guò)控制臺(tái)查看數(shù)據(jù)是否正常獲?。?/p>

2.2.2 通過(guò)后臺(tái)獲取的數(shù)據(jù)構(gòu)建菜單導(dǎo)航
先構(gòu)建一級(jí)導(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>頁(yè)面效果:

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

<!--
使用v-for生成二級(jí)導(dǎo)航菜單,index為功能url值,二級(jí)菜單為葉子節(jié)點(diǎn),為具體功能的功能菜單,
所以u(píng)rl一定有值(一級(jí)菜單的url為空)。
測(cè)試數(shù)據(jù)二級(jí)菜單沒(méi)有分組,所以不用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>頁(yè)面效果:

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

2.3.2 配置路由

2.3.3 修改LeftAside組件

2.3.4 修改Main組件

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

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

配置首頁(yè)菜單:

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

<!--設(shè)置首頁(yè)菜單及其圖標(biāo),index設(shè)置的是Home組件的path-->
<el-menu-item key="home" index="/Home">
<i class="el-icon-s-home"></i>
<span>首頁(yè)</span>
</el-menu-item>4. 表格數(shù)據(jù)顯示
4.1 頁(yè)面布局
頁(yè)面上使用的面包屑,查詢(xún)條件,表格,分頁(yè)等空間,可以查看element-ui官網(wǎng)。該步驟主要關(guān)注頁(yè)面布局,并沒(méi)有綁定數(shù)據(jù),編寫(xiě)完成后,觀(guān)察頁(yè)面效果。
BookList.vue:
<template>
<div style="margin-left: 15px; margin-right: 15px;">
<!--面包屑-->
<el-breadcrumb style="margin-top:15px;" separator="/">
<el-breadcrumb-item :to="{path: '/Home'}">首頁(yè)</el-breadcrumb-item>
<el-breadcrumb-item>書(shū)本管理</el-breadcrumb-item>
</el-breadcrumb>
<!--查詢(xún)條件-->
<el-form style="margin-top: 15px;" :inline="true" class="demo-form-inline">
<el-form-item label="書(shū)名">
<el-input placeholder="書(shū)名"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary">查詢(xún)</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="編號(hào)" min-width="40" align="center"></el-table-column>
<el-table-column prop="bookname" label="名稱(chēng)" 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="類(lèi)型" min-width="70" align="center"></el-table-column>
</el-table>
<!--分頁(yè)-->
<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 查詢(xún)并在表格中顯示數(shù)據(jù)
先不考慮分頁(yè),從后臺(tái)接口獲取數(shù)據(jù)并綁定到表格顯示。
將查詢(xún)書(shū)本信息的接口配置到api/action.js中
//獲取書(shū)本信息 '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)分頁(yè)
template部分:

<!--分頁(yè)-->
<div class="block" style="text-align:right;margin-top:10px;">
<!--
@size-chang: 定義在每頁(yè)顯示的記錄數(shù)變化時(shí)的處理函數(shù)
@current-change:當(dāng)前頁(yè)碼發(fā)生變化時(shí)的處理函數(shù),如點(diǎn)擊頁(yè)碼或輸入一個(gè)特定頁(yè)碼。
:current-page:指定當(dāng)前頁(yè),
:page-size:每頁(yè)顯示的記錄數(shù)
layout: 布局,可以通過(guò)調(diào)整該項(xiàng)來(lái)調(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,
//分頁(yè)參數(shù)
page: this.page,
rows: this.rows
}).then(resp => {
console.log(resp);
this.books = resp.data.data;
//獲取總頁(yè)數(shù)
this.total = resp.data.total;
}).catch(error => {
console.log(error);
});
}script部分,圖二:

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

