Element-UI踩坑之Pagination組件的使用
先說結(jié)論:在改變pageSize時,若當(dāng)前的currentPage超過了最大有效值,就會修改為最大有效值。
一般Pagination組件的聲明如下:
<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-size="pageSize" :current-page="currentPage" :total="total" :page-sizes="[10, 20, 50, 100, 200, 300, 400]" layout="total, sizes, prev, pager, next, jumper"> </el-pagination>
數(shù)據(jù)都是異步獲取的,所以會定義一個獲取數(shù)據(jù)的方法:
getData() {
const params = {
pageSize: this.pageSize,
currentPage: this.currentPage
};
get(params).then(res => {
if (res.status === 0) {
...
this.total = res.result.count;
}
});
}
一般我們會在pageSize或currentPage改變時,再次去獲取新數(shù)據(jù):
handleSizeChange(val) {
this.pageSize = val;
this.getData();
},
handleCurrentChange(val) {
this.currentPage = val;
this.getData();
}
以上都符合常理,看起來沒什么問題!但是,來看以下這種特殊情況:
假設(shè)有473條數(shù)據(jù),即total = 473
當(dāng)前pageSize = 10, pageCount = Math.ceil(473 / 10) = 48, currentPage = 48
現(xiàn)在將pageSize = 200,則pageCount = Math.ceil(473 / 200) = 3
這時奇怪的事情就發(fā)生了,首先頁面的表現(xiàn)為:先是無數(shù)據(jù),然后過一會數(shù)據(jù)才加載。
打開控制臺查看網(wǎng)絡(luò)請求,發(fā)現(xiàn)獲取了兩次數(shù)據(jù)!
查看請求參數(shù),第一次為:pageSize: 200, currentPage : 48
第二次為:pageSize: 200, currentPage: 3
這好像可以解釋了,為什么請求了兩次數(shù)據(jù)?因?yàn)閜ageSize與currentPage的改變都會觸發(fā)事件去請求數(shù)據(jù)。
但是!pageSize是我們手動改變的,那currentPage呢?
查看整個組件內(nèi)可能觸發(fā)currentPage的行為,但并沒有。
那只有一種可能,就是Element-UI庫內(nèi)部幫我們修改的!
秉著不求甚解的理念,去查看了Element-UI中Pagination組件的源碼:
其中currentPage在Pagination組件內(nèi)叫 internalCurrentPage
watch: {
internalCurrentPage: {
immediate: true,
handler(newVal, oldVal) {
newVal = parseInt(newVal, 10);
/* istanbul ignore if */
if (isNaN(newVal)) {
newVal = oldVal || 1;
} else {
// 注意這里
newVal = this.getValidCurrentPage(newVal);
}
if (newVal !== undefined) {
this.internalCurrentPage = newVal;
if (oldVal !== newVal) {
this.$emit('currentPage', newVal);
}
} else {
this.$emit('currentPage', newVal);
}
}
}
}
注意我注釋標(biāo)明的地方:
newVal = this.getValidCurrentPage(newVal)
方法名getValidCurrentPage,顧名思義 獲取有效的當(dāng)前頁
以上兩點(diǎn)足以證明,Element-UI中的Pagination組件會修改currentPage為一個有效值!
具體看看getValidCurrentPage方法的實(shí)現(xiàn):
getValidCurrentPage(value) {
value = parseInt(value, 10);
const havePageCount = typeof this.internalPageCount === 'number';
let resetValue;
if (!havePageCount) {
if (isNaN(value) || value < 1) resetValue = 1;
} else {
if (value < 1) {
resetValue = 1;
} else if (value > this.internalPageCount) {
// 注意這里
resetValue = this.internalPageCount;
}
}
if (resetValue === undefined && isNaN(value)) {
resetValue = 1;
} else if (resetValue === 0) {
resetValue = 1;
}
return resetValue === undefined ? value : resetValue;
}
重點(diǎn)看這句代碼:
else if (value > this.internalPageCount) {
resetValue = this.internalPageCount;
}
這里就是我們遇到的特殊情況,在改變pageSize時,若當(dāng)前的currentPage超過了最大有效值,就會修改為最大有效值!
其實(shí)Element-UI修改的說法并不正確,它只是向上派發(fā)了事件,最終修改值的是我們自己。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)用戶動態(tài)權(quán)限登錄的代碼示例
這篇文章主要介紹了vue如何實(shí)現(xiàn)用戶動態(tài)權(quán)限登錄,文中的代碼示例介紹的非常詳細(xì),對大家學(xué)習(xí)vue有一定的幫助,需要的朋友可以參考閱讀2023-05-05
在vue中使用echarts實(shí)現(xiàn)飛機(jī)航線水滴圖詞云圖效果
這篇文章主要介紹了在vue中使用echarts實(shí)現(xiàn)飛機(jī)航線?水滴圖?詞云圖,通過引入中國地圖JS文件,會自動注冊地圖,文中結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
vue 純js監(jiān)聽滾動條到底部的實(shí)例講解
今天小編就為大家分享一篇vue 純js監(jiān)聽滾動條到底部的實(shí)例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue實(shí)現(xiàn)導(dǎo)出excel表格功能
這篇文章主要介紹了Vue實(shí)現(xiàn)導(dǎo)出excel表格的功能,在文章末尾給大家提到了vue中excel表格的導(dǎo)入和導(dǎo)出代碼,需要的朋友可以參考下2018-03-03
Vue監(jiān)聽localstorage變化的方法詳解
在日常開發(fā)中,我們經(jīng)常使用localStorage來存儲一些變量,這些變量會存儲在瀏覽中,對于localStorage來說,即使關(guān)閉瀏覽器,這些變量依然存儲著,方便我們開發(fā)的時候在別的地方使用,本文就給大家介紹Vue如何監(jiān)聽localstorage的變化,需要的朋友可以參考下2023-10-10
vue用BMap百度地圖實(shí)現(xiàn)即時搜索功能
這篇文章主要為大家詳細(xì)介紹了vue用BMap百度地圖實(shí)現(xiàn)即時搜索功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09
vue結(jié)合leaflet實(shí)現(xiàn)熱力圖
本文主要介紹了vue實(shí)現(xiàn)熱力圖,結(jié)合leaflet.heat插件可以很容易的做出熱力圖,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

