一篇文章告訴你如何實現(xiàn)Vue前端分頁和后端分頁
更新時間:2021年12月31日 17:08:37 作者:lxslxskxs
這篇文章主要為大家介紹了如何實現(xiàn)Vue前端分頁和后端分頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
1:前端手寫分頁(數(shù)據(jù)量小的情況下)
前端需要使用slice截?。?tableData((page-1)pageSize,pagepageSize)


2:后端分頁,前端只需要關(guān)注傳遞的page和pageSize
3:前端手寫分頁按鈕
<body>
<div id="app">
<table class="table table-bordered table-condensed">
<tr class="bg-primary">
<th class="text-center">排序</th>
<th class="text-center">用戶姓名</th>
<th class="text-center">用戶性別</th>
<th class="text-center">所在城市</th>
</tr>
<tr class="text-center active" v-for="(v,i) in list" :key="i">
<td>{{num(i)}}</td>
<!-- <td>{{params.pagesize}}</td> -->
<td>{{v.name}}</td>
<td>{{v.sex}}</td>
<td>{{v.addr}}</td>
</tr>
</table>
<nav aria-label="Page navigation" style="text-align: center;">
<ul class="pagination">
<!-- 上一頁 -->
<li @click="prePage()" :class="{'disabled':params.page == 1}">
<a aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li :class="{'active': params.page == page}" v-for="(page,index) in pages" :key="index" @click="curPage(page)">
<a style="cursor: pointer;">
{{page}}
</a>
</li>
<!-- 下一頁 -->
<li :class="{'disabled':params.page == totalPage}" @click="next()">
<a aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</div>
</body>
window.onload = function () {
// 1s內(nèi)只允許發(fā)送請求(出發(fā)事件)一次(可多次點擊) 節(jié)流 throttle
new Vue({
el: '#app',
data: {
params:{
page:1,
pagesize:20,
name:''
},
list: [],
total:0,//總的條數(shù)
totalPage:0,//總的頁數(shù)
flag: false,
},
created() {
this.getData()
},
computed: {
pages() {
let totalPage = this.totalPage;
let page = this.params.page;
// 總的頁數(shù)小于10頁
if(totalPage < 10) return totalPage;
// 總的頁數(shù)大于10頁添加省略號
if(page <= 5) { // 前五頁
// (1) 頁碼小于5 左邊顯示六個
return [1,2,3,4,5,6,'...',totalPage]
} else if (page >= totalPage - 5) { // 后五頁
console.log("觸發(fā)")
// (2) 頁碼大于總頁數(shù)-5 右邊顯示六個
return [1,'...',totalPage-5,totalPage-4,totalPage-3,totalPage-2,totalPage-1,totalPage]
} else { // 中間五頁
// (3)頁碼在 5-(totalPage-5)之間 左邊區(qū)間不能小于5 右邊區(qū)間不能大于總頁數(shù)totalPage,注意 左邊的當前頁-num 不能小于1, 右邊的當前頁+num不能大于總頁數(shù)
return [1,'...',page-1,page,page+1,page+2,page+3,'...',totalPage]
}
},
num() {
let { pagesize, page} = this.params
// (1-1) * 10 + 10 + 0 + 1 = 1;
// (2-1) * 10 + 10 + 0 + 1 = 11
// 第一頁 = (當前頁 -1 )* 每頁的條數(shù) + 索引值 + 1 保證是從1開始的
return i => (page - 1) * pagesize + i + 1 // (當前頁- 1 * 每頁的條數(shù)) + 索引值 + 1
}
},
methods: {
getData() {
if(this.flag) return;
this.flag = true;
// 下面就是相當于一個定時器
axios.get('http://localhost:3000/user/listpage',{params:this.params}).then(res => {
console.log('res',res.data.users)
let { total,users } = res.data.users;
this.total = total;
this.totalPage = Math.ceil( this.total / this.params.pagesize);
this.list = users
this.flag = false;
})
},
curPage(page) {
if(page == '...') return
if(this.flag) return;
this.params.page = page;
this.getData()
},
prePage() {
// if(this.params.page == '...') return
if(this.params.page > 1) {
if(this.flag) return;
--this.params.page;
console.log('page',this.params.page)
this.getData()
}
},
next() {
// if(this.params.page == '...') return
if(this.flag) return;
console.log("執(zhí)行",this.totalPage)
if(this.params.page < this.totalPage) {
++this.params.page;
console.log('page',this.params.page)
this.getData()
}
},
}
})
}

總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue項目中chunk-vendors.js提示報錯的查看方法(最新推薦)
在vue項目中,chunk-vendors.js報出的錯誤提示經(jīng)常會導(dǎo)致開發(fā)者困惑,正確查看錯誤的方法是從錯誤提示的詳細信息中找到報錯原因,下面給大家分享vue項目中chunk-vendors.js提示報錯的查看方法,感興趣的朋友一起看看吧2024-12-12
Vue中通過Vue.extend動態(tài)創(chuàng)建實例的方法
這篇文章主要介紹了Vue中通過Vue.extend動態(tài)創(chuàng)建實例的方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
Vue+TailWindcss實現(xiàn)一個簡單的闖關(guān)小游戲
本文將利用Vue+TailWindcss實現(xiàn)一個簡單的闖關(guān)小游戲,玩家須躲避敵人與陷阱到達終點且擁有多個關(guān)卡,感興趣的小伙伴可以了解一下2022-04-04
vue.js vue-router如何實現(xiàn)無效路由(404)的友好提示
眾所周知vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構(gòu)建單頁面應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于vue.js中vue-router如何實現(xiàn)無效路由(404)的友好提示的相關(guān)資料,需要的朋友可以參考下。2017-12-12

