因elementUi分頁封裝成單獨組件當前頁存在bug,沒有在父組件設置當前頁。 如有需要可在分頁組件單獨設置
在使用頁面(父頁面)
<!-- 向子組件傳total值(總數),接收子組件sizeChange事件(每頁顯示條數),接收子組件currentChange事件(當前頁) -->
<paging
:total="querycdt.total"
@sizeChange="handleSizeChange"
@currentChange="handleCurrentChange">
</paging>
components:{
paging : require('@/components/common/paging.vue').default, // 引入分頁組件
}
// 每頁顯示多少條
handleSizeChange(val){
this.querycdt.pageLength = val // 每頁多少條
this.getUserList() // 刷新
},
// 當前頁
handleCurrentChange(val) {
this.querycdt.pagenum = val // 當前頁
this.getUserList() // 刷新
}
在 paging 組件頁面
<template>
<div>
<el-pagination
@size-change='handleSizeChange'
@current-change="handleCurrentChange"
:page-sizes="[10,20,30]"
:page-size="10"
layout="total, prev, pager, next, jumper"
:total="querycdt.total">
</el-pagination>
</div>
</template>
<script>
export default {
props: ['total'],
methods:{
handleSizeChange(val){
// console.log(`每頁${val}條`)
// 向父組件傳遞sizeChange事件,傳入val值
this.$emit('sizeChange',val)
},
handleCurrentChange(val){
// console.log(`當前頁:${val}`)
this.$emit('currentChange',val)
}
}
}
</script>
<style lang="less" scoped>
</style>
刪除頁面最后一條數據時,currentPage沒有減一,頁面列表為空
思路:監聽頁面的總條數,并對總條數進行判斷。
當頁面總條數 = (當前頁數-1)*當前頁條數,currentPage減1,重新獲取列表
props: ['querycdt'], // 直接接收父組件傳過來的querycdt分頁對象
// querycdt.totalNum 數據總條數
// querycdt.currentPage 當前頁
// querycdt.pageSize 每頁顯示的數據條數
watch: {
'querycdt.currentPage':{ //監聽props傳過來的querycdt對象的currentPage(當前頁)
handler(){ // 隨便寫在一個函數內
if(this.querycdt.totalNum==(this.querycdt.currentPage-1)*this.querycdt.pageSize&& this.querycdt.totalNum!=0){ //這一行是關鍵代碼,倍數關系
this.querycdt.currentPage -= 1
this.geiList()//獲取表格數據的方法
}
}
}
},
額外說一點,數據的每個序號,一般用
item.index= this.querycdt.pageSize * (this.querycdt.currentPage - 1) + (index + 1);