二次封裝element ui pagination組件方式
更新時間:2025年11月22日 15:11:11 作者:活寶小娜
文章總結了在Vue2中二次封裝Element UI Pagination組件的過程,包括HTML、JS和CSS部分的代碼示例,作者分享了個人經(jīng)驗,旨在為讀者提供參考,并鼓勵大家支持腳本之家
vue2中二次封裝element ui pagination組件
html部分
<template>
<div class="table-pagination">
<el-pagination
:current-page.sync="currentPage"
:page-sizes="pageSizes"
:page-size="pageSize"
:layout="paginationLayout"
:total="total"
background
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>
js部分
<script>
export default {
name: "DiyPagination",
props: {
// 分頁配置
pagination: {
type: Object,
default: null
},
},
data() {
return {
currentPage: 1,
pageSize: 10,
total: 0,
pageSizes: [10, 20, 50, 100],
paginationLayout: 'total, sizes, prev, pager, next, jumper',
};
},
watch: {
// 監(jiān)聽分頁配置變化
pagination: {
immediate: true,
deep: true,
handler(val) {
if (val) {
this.currentPage = val.currentPage || 1;
this.pageSize = val.pageSize || 10;
this.total = val.total || 0;
this.pageSizes = val.pageSizes || [10, 20, 50, 100];
this.paginationLayout = val.layout || 'total, sizes, prev, pager, next, jumper';
}
}
}
},
methods: {
// 處理分頁大小變化
handleSizeChange(size) {
this.pageSize = size;
this.$emit('pagination-change', {
pageSize: size,
currentPage: this.currentPage
});
},
// 處理當前頁變化
handleCurrentChange(page) {
this.currentPage = page;
this.$emit('pagination-change', {
pageSize: this.pageSize,
currentPage: page
});
},
}
}
</script>
css部分
.table-pagination {
padding: 16px;
display: flex;
justify-content: flex-end;
border-top: 1px solid #ebeef5;
background-color: #fff;
}
在組件中使用
<template>
<DiyPagination
:pagination="pagination"
@pagination-change="paginationChange"
></DiyPagination>
</template>
<script>
import DiyPagination from "@/components/DiyPagination/index.vue"
export default {
components: {
DiyPagination
},
data() {
return {
pagination: { // 表格分頁配置
currentPage: 1,
pageSize: 20,
total: null,
pageSizes: [20, 50, 100],
layout: 'total, sizes, prev, pager, next, jumper',
},
}
},
methods: {
paginationChange({pageSize, currentPage}) {
this.pagination.currentPage = currentPage
this.pagination.pageSize = pageSize
},
}
}
</script>
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Echarts+VUE柱狀圖繪制細節(jié)并且屏幕自適應完整代碼
柱狀圖(或稱條形圖)是一種通過柱形的長度來表現(xiàn)數(shù)據(jù)大小的一種常用圖表類型,這篇文章主要給大家介紹了關于Echarts+VUE柱狀圖繪制細節(jié)并且屏幕自適應的相關資料,需要的朋友可以參考下2024-02-02
VUE3開箱即用的音頻播放組件完整代碼(基于原生audio)
Vue3開箱即用的框架有很多選擇,下面這篇文章主要介紹了VUE3開箱即用的音頻播放組件(基于原生audio)的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-09-09

