vue實(shí)現(xiàn)分頁功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)分頁功能的具體代碼,供大家參考,具體內(nèi)容如下
- 使用組件分頁
- 自己寫分頁
一、組件分頁
<el-pagination layout="prev, pager, next" @current-change="changePageNum" :current-page="pageNum" :page-size="pageSize" :total="total"> </el-pagination>
data(){ return{ tableData: [], total: 0,//總數(shù) pageNum: 1,//當(dāng)前頁 pageSize: 15,//每頁顯示數(shù)量 } } mounted: function () { this.query();//加載頁面時(shí),獲取數(shù)據(jù) }, methods:{ //切換頁碼 changePageNum: function (val) { this.pageNum = val; this.query(); }, //通過接口,獲取數(shù)據(jù) query: function () { var data = { name: this.name || '', fleetId: this.FleetId, pageNum: this.pageNum,//當(dāng)前頁 pageSize: this.pageSize//查詢個(gè)數(shù) }; RoleManage.getRole(data) .then(res => { var data = res; if (res.success) { this.tableData = data.obj.list; this.total = data.obj.total; this.name =''; } else { this.$message('查詢失敗'); } }).catch(err => { // 異常情況 console.log(err); }); }, }
組件分頁效果
二、自己構(gòu)建分頁
有些時(shí)候需要根據(jù)需求自己寫分頁
1、分頁樣式
2、上下切頁
//調(diào)度-系統(tǒng)通訊錄分頁 dispatchCourentPage: 1, //調(diào)度-系統(tǒng)通訊錄當(dāng)前選中標(biāo)簽標(biāo)記 dispatchCourentIndex: 1, //調(diào)度-系統(tǒng)通訊錄更多標(biāo)記項(xiàng):組id dispatchMorecommandGroupId: '', dispatchTotlePage: 0, //上頁 handleLastPage() { if (this.dispatchCourentPage === 1) return; this.dispatchCourentPage -= 1; let index = this.dispatchCourentIndex; if (this.dispatchMorecommandGroupId != '') { this.queryBookMoreBygroupId(this.dispatchMorecommandGroupId); } else { this.queryBookmember(index); } }, //下頁 handleNextPage() { if (this.dispatchCourentPage === this.dispatchTotlePage) return; this.dispatchCourentPage += 1; let index = this.dispatchCourentIndex; if (this.dispatchMorecommandGroupId != '') { this.queryBookMoreBygroupId(this.dispatchMorecommandGroupId); } else { this.queryBookmember(index); } }
三、使用監(jiān)聽屬性控制分頁顯示
computed: { limitData() { let data = [...this.table1Datas]; return data; }, // 因?yàn)橐獎(jiǎng)討B(tài)計(jì)算總頁數(shù),所以還需要一個(gè)計(jì)算屬性來返回最終給 Table 的 data dataWithPage() { const data = this.limitData; const start = this.current * this.size - this.size; const end = start + this.size; return [...data].slice(start, end); }, }
四、js控制分頁,計(jì)算總頁數(shù)
方法1
/** *根據(jù)數(shù)據(jù)條數(shù)與每頁多少條數(shù)據(jù)計(jì)算頁數(shù) * totalnum 數(shù)據(jù)條數(shù) * limit 每頁多少條 */ pageCount(totalnum, limit) { return totalnum > 0 ? ((totalnum < limit) ? 1 : ((totalnum % limit) ? (parseInt(totalnum / limit) + 1) : (totalnum / limit))) : 0; },
方法2
/** * 分頁的總頁數(shù)算法 * 總記錄數(shù):totalRecord * 每頁最大記錄數(shù):maxResult */ function pageCount() { totalPage = (totalRecord + maxResult -1) / maxResult; }
方法3 推薦
totalPage = Math.floor((totalRecord +maxResult -1) /maxResult );
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3?$emit用法指南(含選項(xiàng)API、組合API及?setup?語法糖)
這篇文章主要介紹了Vue3?$emit用法指南,使用?emit,我們可以觸發(fā)事件并將數(shù)據(jù)傳遞到組件的層次結(jié)構(gòu)中,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Vue使用electron轉(zhuǎn)換項(xiàng)目成桌面應(yīng)用方法介紹
Electron也可以快速地將你的網(wǎng)站打包成一個(gè)原生應(yīng)用發(fā)布,下面這篇文章主要給大家介紹了關(guān)于Vue和React中快速使用Electron的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11el-table實(shí)現(xiàn)給每行添加loading效果案例
這篇文章主要介紹了el-table實(shí)現(xiàn)給每行添加loading效果案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Vue中select下拉框的默認(rèn)選中項(xiàng)的三種情況解讀
這篇文章主要介紹了Vue中select下拉框的默認(rèn)選中項(xiàng)的三種情況解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05關(guān)于this.$refs獲取不到dom的可能原因及解決方法
這篇文章主要介紹了關(guān)于this.$refs獲取不到dom的可能原因及解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Vue前端實(shí)現(xiàn)截圖功能的簡(jiǎn)單步驟
本文介紹了如何使用html2canvas庫來實(shí)現(xiàn)HTML頁面或某個(gè)元素的截圖功能,文中通過代碼介紹的非常詳細(xì),需要注意的是此方法只能在瀏覽器環(huán)境中使用,需要的朋友可以參考下2024-10-10