本文實(shí)例為大家分享了vue+Element-ui實(shí)現(xiàn)分頁(yè)效果的具體代碼,供大家參考,具體內(nèi)容如下
當(dāng)我們向后臺(tái)請(qǐng)求大量數(shù)據(jù)的時(shí)候,并要在頁(yè)面展示出來(lái),請(qǐng)求的數(shù)據(jù)可能上百條數(shù)據(jù)或者更多的時(shí)候,并不想在一個(gè)頁(yè)面展示,這就需要使用分頁(yè)功能來(lái)去完成了。
1.本次所使用的是vue2.0+element-ui實(shí)現(xiàn)一個(gè)分頁(yè)功能,element-ui這個(gè)組件特別豐富,分頁(yè)中給我提供了一個(gè)Pagination 分頁(yè),使用Pagination 快速完成分頁(yè)功能
最終效果展示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
< div class = "deit" > < div class = "crumbs" > < el-breadcrumb separator = "/" > < el-breadcrumb-item >< i class = "el-icon-date" ></ i > 數(shù)據(jù)管理</ el-breadcrumb-item > < el-breadcrumb-item >用戶列表</ el-breadcrumb-item > </ el-breadcrumb > < div class = "cantainer" > < el-table style = "width: 100%;" :data = "userList.slice((currentPage-1)*pagesize,currentPage*pagesize)" //對(duì)數(shù)據(jù)請(qǐng)求的處理,最為重要的一句話 > < el-table-column type = "index" width = "50" > </ el-table-column > < el-table-column label = "日期" prop = "date" width = "180" > </ el-table-column > < el-table-column label = "用戶姓名" prop = "name" width = "180" > </ el-table-column > < el-table-column label = "郵箱" prop = "email" width = "180" > </ el-table-column > < el-table-column label = "地址" prop = "address" width = "200" > </ el-table-column > </ el-table > < el-pagination @ size-change = "handleSizeChange" @ current-change = "handleCurrentChange" :current-page = "currentPage" :page-sizes = "[5, 10, 20, 40]" //這是下拉框可以選擇的,每選擇一行,要展示多少內(nèi)容 :page-size = "pagesize" //顯示當(dāng)前行的條數(shù) layout = "total, sizes, prev, pager, next, jumper" :total = "userList.length" > //這是顯示總共有多少數(shù)據(jù), </ el-pagination > </ div > </ div > </ div > |
需要data定義一些,userList定義一個(gè)空數(shù)組,請(qǐng)求的數(shù)據(jù)都是存放這里面
1
2
3
4
5
6
7
|
data () { return { currentPage:1, //初始頁(yè) pagesize:10, // 每頁(yè)的數(shù)據(jù) userList: [] } }, |
對(duì)一些數(shù)據(jù),方法處理,數(shù)據(jù)的來(lái)源是自己通過(guò)json-server搭建的本地?cái)?shù)據(jù),通過(guò)vue-resource請(qǐng)求數(shù)據(jù),
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
created() { this .handleUserList() }, methods: { // 初始頁(yè)currentPage、初始每頁(yè)數(shù)據(jù)數(shù)pagesize和數(shù)據(jù)data handleSizeChange: function (size) { this .pagesize = size; console.log( this .pagesize) //每頁(yè)下拉顯示數(shù)據(jù) }, handleCurrentChange: function (currentPage){ this .currentPage = currentPage; console.log( this .currentPage) //點(diǎn)擊第幾頁(yè) }, handleUserList() { this .$http.get( 'http://localhost:3000/userList' ).then(res => { //這是從本地請(qǐng)求的數(shù)據(jù)接口, this .userList = res.body }) } } |
以上都是分頁(yè)所需的功能,也是自己在自己寫(xiě)案例中所遇到的,并總結(jié)下方便查看,喜歡的可以關(guān)注一下
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/yusirxiaer/article/details/103201728