vue?el-pagination分頁查詢封裝的示例代碼
需求:因?yàn)樾枰玫奖韱尾樵兊牡胤教嗔耍詾榱吮苊饷總€(gè)頁面都寫分頁組件,直接封裝好調(diào)用就完事了,簡(jiǎn)簡(jiǎn)單單
1.創(chuàng)建Pigination.vue公用組件
<template>
<div :class="{'hidden':hidden}" class="pagination-container">
<el-pagination
:background="background"
:current-page.sync="currentPage"
:page-size.sync="pageSize"
:layout="layout"
:page-sizes="pageSizes"
:total="total"
v-bind="$attrs"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
></el-pagination>
</div>
</template>
<script>
export default {
name: 'Pagination',
props: {
total: {
required: true,
type: Number
},
// 當(dāng)前頁數(shù)
page: {
type: Number,
default: 1
},
// 限制,一頁多多少行
limit: {
type: Number,
default: 20
},
// 分頁
pageSizes: {
type: Array,
default() {
return [10, 20, 30, 50, 100]
}
},
layout: {
type: String,
default: 'total, sizes, prev, pager, next, jumper'
},
background: {
type: Boolean,
default: true
},
autoScroll: {
type: Boolean,
default: true
},
hidden: {
type: Boolean,
default: false
}
},
computed: {
// 當(dāng)前頁數(shù)
currentPage: {
get() {
return this.page;
},
set(val) {
this.$emit('update:page', val);
}
},
pageSize: {
get() {
return this.limit;
},
set(val) {
this.$emit('update:limit', val);
}
}
},
methods: {
handleSizeChange(val) {
this.$emit('pagination', { page: this.currentPage, size: val });
// if (this.autoScroll) {
// scrollTo(0, 800);
// }
},
handleCurrentChange(val) {
this.$emit('pagination', { page: val, size: this.pageSize });
// if (this.autoScroll) {
// scrollTo(0, 800);
// }
}
}
}
</script>
<style scoped>
.pagination-container {
background: #fff;
padding-top: 10px;
}
.pagination-container.hidden {
display: none;
}
</style>2.需要使用分頁組件的頁面調(diào)用
<template>
<div class="content-box">
<div class="container">
<p>主體頁面 2 - 2</p>
<div class="table-container">
<el-table
ref="Table2"
:data="list"
style="width: 100%;"
show-overflow-tooltip="true"
@selection-change="handleSelectionChange"
border
>
<el-table-column type="selection" align="center"></el-table-column>
<el-table-column label="操作" width="120" align="center" fixed="right"> </el-table-column>
</el-table>
</div>
<!-- 分頁組件 -->
<Pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList"></Pagination>
</div>
</div>
</template>
<script>
import Pagination from '@/components/common/Pagination';
import { testApi } from '@/api/test';
const defaultListQuery = {
pageNum: 1,//給后端的數(shù)據(jù),可以根據(jù)后端需要更改
pageSize: 10,
keyword: ''
};
export default {
data() {
return {
listQuery: Object.assign({}, defaultListQuery),//淺拷貝,引用數(shù)據(jù)類型相互影響
list: [],
total: 0,
listLoading: true,
selectedList: []
};
},
components: {
// 注冊(cè)分頁
Pagination
},
created() {
this.getList();
},
methods: {
getList() {
// 獲取數(shù)據(jù)列表
this.listLoading = true;
testApi(this.listQuery).then(res => {
this.listLoading = false;
this.list = res.data.list;
this.total = res.data.total;
});
},
handleSelectionChange(val) {
// 批量選擇行數(shù)
this.selectedList = val;
}
}
};
</script>3.結(jié)果

到此這篇關(guān)于vue el-pagination分頁查詢封裝的文章就介紹到這了,更多相關(guān)vue el-pagination分頁查詢封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)滾動(dòng)條下滑時(shí)隱藏導(dǎo)航欄,上滑時(shí)顯示導(dǎo)航欄功能
這篇文章主要介紹了vue實(shí)現(xiàn)滾動(dòng)條下滑時(shí)隱藏導(dǎo)航欄,上滑時(shí)顯示導(dǎo)航欄,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07
vue長(zhǎng)列表優(yōu)化之虛擬列表實(shí)現(xiàn)過程詳解
前端的業(yè)務(wù)開發(fā)中會(huì)遇到不使用分頁方式來加載長(zhǎng)列表的需求,下面這篇文章主要給大家介紹了關(guān)于vue長(zhǎng)列表優(yōu)化之虛擬列表實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
vue 組件數(shù)據(jù)加載解析順序的詳細(xì)代碼
Vue.js的解析順序可以概括為:模板編譯、組件創(chuàng)建、數(shù)據(jù)渲染、事件處理和生命周期鉤子函數(shù)執(zhí)行,接下來通過本文給大家介紹vue 組件數(shù)據(jù)加載解析順序的完整代碼,感興趣的朋友跟隨小編一起看看吧2024-03-03
vue2+tracking實(shí)現(xiàn)PC端的人臉識(shí)別示例
本文主要介紹了vue2+tracking實(shí)現(xiàn)PC端的人臉識(shí)別示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Vue中路由參數(shù)與查詢參數(shù)傳遞對(duì)比解析
在Vue.js中,路由與導(dǎo)航不僅涉及頁面切換,還包括了向頁面?zhèn)鬟f參數(shù)和獲取查詢參數(shù),這篇文章主要介紹了Vue路由參數(shù)與查詢參數(shù)傳遞,需要的朋友可以參考下2023-08-08
vue+elemet實(shí)現(xiàn)表格手動(dòng)合并行列
這篇文章主要為大家詳細(xì)介紹了vue+elemet實(shí)現(xiàn)表格手動(dòng)合并行列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
vue使用echarts實(shí)現(xiàn)三維圖表繪制
這篇文章主要為大家詳細(xì)介紹了vue如何在項(xiàng)目中使用echarts實(shí)現(xiàn)三維圖表的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-08-08
Vue中filter使用及根據(jù)id刪除數(shù)組元素方式
這篇文章主要介紹了Vue中filter使用及根據(jù)id刪除數(shù)組元素方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

