123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <template>
- <div :class="{ hidden: hidden }" class="pagination-container">
- <el-pagination :background="background" v-model:current-page="currentPage" v-model:page-size="pageSize"
- :layout="layout" :page-sizes="pageSizes" :pager-count="pagerCount" :total="total" :small="small"
- @size-change="handleSizeChange" @current-change="handleCurrentChange" />
- </div>
- </template>
- <script lang="ts" name="pagination" setup>
- import {computed, onMounted, ref, watch} from 'vue';
- import mittBus from '@/utils/mitt';
- // 定义父组件传过来的值
- const props = defineProps({
- total: {
- required: true,
- type: Number,
- },
- page: {
- type: Number,
- default: 1,
- },
- limit: {
- type: Number,
- default: 20,
- },
- pageSizes: {
- type: Array,
- default() {
- return [10, 20, 30, 50, 100]
- },
- },
- // 移动端页码按钮的数量端默认值5
- pagerCount: {
- type: Number,
- default: document.body.clientWidth < 992 ? 5 : 7,
- },
- layout: {
- type: String,
- default: 'total, sizes, prev, pager, next, jumper',
- },
- background: {
- type: Boolean,
- default: false,
- },
- autoScroll: {
- type: Boolean,
- default: true,
- },
- hidden: {
- type: Boolean,
- default: false,
- },
- })
- // 定义子组件向父组件传值/事件
- const emit = defineEmits(['update:page', 'update:limit', 'pagination']);
- // 定义变量内容
- const small = ref(false);
- const currentPage = computed({
- get() {
- return props.page
- },
- set(val) {
- emit('update:page', val)
- },
- })
- const pageSize = computed({
- get() {
- return props.limit
- },
- set(val) {
- emit('update:limit', val)
- },
- })
- const handleSizeChange = (val: any) => {
- /**
- * 场景:API返回总数为25条,按照10条每页,一共有3页。选了2的页数之后,然后把size选择成30条,
- * 这个时候,分页就会同时触发size选择函数以及current选择函数。{page: 2, size: 30},{page: 1, size: 30}请求两次,会导致列表会有暂无数据的情况
- * 解决办法:用setTimeout(函数,0),用延迟,虽然还是两次请求,但是每次都是{page: 1, size: 30}
- */
- setTimeout(() => {
- emit('pagination', { page: currentPage.value, limit: val })
- }, 0)
- mittBus.emit('scrollTopEmit', { top: 0 });// 分页发送监听事件 滚动到页面顶部
- }
- const handleCurrentChange = (val: any) => {
- emit('pagination', { page: val, limit: pageSize.value })
- mittBus.emit('scrollTopEmit', { top: 0 });// 分页发送监听事件 滚动到页面顶部
- }
- onMounted(() => {
- // 监听布局大小 改变分页的大小
- // let themeConfig = Local.get('themeConfig');
- // small.value = themeConfig.globalComponentSize == 'small';
- })
- </script>
- <style scoped>
- .pagination-container {
- padding-top: 20px;
- display: flex;
- justify-content: flex-end;
- }
- .pagination-container.hidden {
- display: none;
- }
- </style>
|