vue實(shí)現(xiàn)簡單的分頁功能
前端Vue實(shí)現(xiàn)分頁功能,供大家參考,具體內(nèi)容如下
我們都知道在spring boot項(xiàng)目中安裝pagehelper可以實(shí)現(xiàn)分頁功能,但是在vue中也能在前端實(shí)現(xiàn)分頁。
1、首先,在data中定義以下變量:
data() {
? ? return {
? ? ? list: null,
? ? ? listLoading: true,
? ? ? totalPage: 1, // 統(tǒng)共頁數(shù),默認(rèn)為1
?? ??? ??? ?currentPage: 1, //當(dāng)前頁數(shù) ,默認(rèn)為1
?? ??? ??? ?pageSize: 5, // 每頁顯示數(shù)量
? ? ? currentPageData: [], //當(dāng)前頁顯示內(nèi)容
? ? ? headPage: 1
? ? }
? },2、發(fā)送請求,獲取后端數(shù)據(jù)(list集合)
axios.get('http://192.168.56.1:8081/sel/'+id).then((res) =>{
? ? ? ? console.log(res.data.data )?
? ? ? ? that.list = res.data.data?
? ? ? ? that.listLoading = false3、根據(jù)返回?cái)?shù)據(jù)list的length來計(jì)算data中變量的值:
this.totalPage=Math.ceil(this.list.length / this.pageSize); ? ? ? ? this.totalPage = this.totalPage == 0 ? 1 : this.totalPage; ? ? ? ? this.getCurrentPageData();
4、調(diào)用getCurrentPageData()方法設(shè)置當(dāng)前頁面的數(shù)據(jù)
getCurrentPageData() {
?? ??? ??? ? ? ? ? ? ? ?let begin = (this.currentPage - 1) * this.pageSize;
?? ??? ??? ? ? ? ? ? ? ?let end = this.currentPage * this.pageSize;
?? ??? ??? ? ? ? ? ? ? ?this.currentPageData = this.list.slice(
?? ??? ??? ? ? ? ? ? ? ? ? ?begin,
?? ??? ??? ? ? ? ? ? ? ? ? ?end
?? ??? ??? ? ? ? ? ? ? ?);
?? ??? ??? ? ? ? ? ?},5、添加按鈕并實(shí)現(xiàn)首頁、尾頁、上一頁、下一頁功能:
<input type="button" value="首頁" @click="firstPage"> <input type="button" value="上一頁" @click="prevPage"> <input type="button" value="下一頁" @click="nextPage"> <input type="button" value="尾頁" @click="lastPage">
?//上一頁
prevPage() {
?? ??? ??? ? ? ? ? ? ?
?? ??? ???if (this.currentPage == 1) {
?? ??? ??? ? ?? ?return false;
?? ??? ??? ??} else {
?? ??? ??? ? ? ? this.currentPage--;
?? ??? ??? ? ? ? this.getCurrentPageData();
?? ??? ??? ? ? ?}
?? ??? ??? ? ? ? ? ?},
?? ??? ??? ? ? ? ? ?// 下一頁
?? ??? ??? ? ? ? ? ?nextPage() {
?? ??? ??? ? ? ? ? ? ? ?if (this.currentPage == this.totalPage) {
?? ??? ??? ? ? ? ? ? ? ? ? ?return false;
?? ??? ??? ? ? ? ? ? ? ?} else {
?? ??? ??? ? ? ? ? ? ? ? ? ?this.currentPage++;
?? ??? ??? ? ? ? ? ? ? ? ? ?this.getCurrentPageData();
?? ??? ??? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? },
? ? ? ? ? ? ? //尾頁
? ? ? ? ? ? ? lastPage() {
?? ??? ??? ? ? if (this.currentPage == this.totalPage) {
?? ??? ??? ? ? ? ? ??return false;
?? ??? ??? ? ? ? ? ? ? ?} else {
?? ??? ??? ? ? ? ? ??this.currentPage=this.totalPage;
?? ??? ??? ? ? ? ? ? this.getCurrentPageData();
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? } ,
? ? ? ? ? ?//首頁
? ? ? ? ? ? ? firstPage(){
? ? ? ? ? ? this.currentPage= ?this.headPage;
? ? ? ? ? ? this.getCurrentPageData();
}注意!
最后需要修改組件中的data

前端展示:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue請求服務(wù)器數(shù)據(jù)后綁定不上的解決方法
今天小編就為大家分享一篇vue請求服務(wù)器數(shù)據(jù)后綁定不上的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Vue微信項(xiàng)目按需授權(quán)登錄策略實(shí)踐思路詳解
這篇文章主要介紹了Vue微信項(xiàng)目按需授權(quán)登錄策略實(shí)踐思路詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05
移動(dòng)端調(diào)試神器vConsole使用詳解
vConsole?是框架無關(guān)的,可以在?Vue、React?或其他任何框架中使用,今天通過本文給大家介紹移動(dòng)端調(diào)試神器vConsole使用,感興趣的朋友一起看看吧2022-04-04
vite.config配置alias Error: ENOTEMPTY: director
這篇文章主要為大家介紹了vite.config配置alias時(shí)報(bào)錯(cuò):Error: ENOTEMPTY: directory not empty, rmdir解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
vue在路由中驗(yàn)證token是否存在的簡單實(shí)現(xiàn)
今天小編就為大家分享一篇vue在路由中驗(yàn)證token是否存在的簡單實(shí)現(xiàn),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Vue.js實(shí)現(xiàn)在下拉列表區(qū)域外點(diǎn)擊即可關(guān)閉下拉列表的功能(自定義下拉列表)
這篇文章主要介紹了Vue.js實(shí)現(xiàn)在下拉列表區(qū)域外點(diǎn)擊即可關(guān)閉下拉列表的功能(自定義下拉列表) ,需要的朋友可以參考下2017-05-05
layui實(shí)際項(xiàng)目使用過程中遇到的兼容性問題及解決
這篇文章主要介紹了layui實(shí)際項(xiàng)目使用過程中遇到的兼容性問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

