vue3纯前端分页Hook封装

本文介绍了如何在Vue项目中,当后端接口不支持分页时,通过封装useUserPaginationhook并配合MDDesign库的md-pagination组件,实现前端动态分页功能,以便满足产品需求。
摘要由CSDN通过智能技术生成

使用场景:做页面时遇到后端接口不支持分页,接口一次性返回表格所有数据,而产品要求前端不能一次性展示,需要有分页功能。
配合mdesign库的md-pagination组件,最终实现的效果如下
在这里插入图片描述步骤:
一、封装分页hook,创建useUserPagination.ts

import { Ref, computed, ref, watch } from 'vue';
export const useUserPagination = <T>(database: Ref<T[]>, pageSize = 10) => {
    // 声明一个data变量,用于存储分页后的数据
    const data = ref<T[]>([]);
    // 声明一个changeData函数,用于分页
    function changeData({ currentPage, currentPageSize }: { currentPage: number; currentPageSize: number }) {
        // 计算当前页面的起始位置
        const start = (currentPage - 1) * currentPageSize;
        // 计算当前页面的结束位置
        const end = start + currentPageSize;
        // 获取当前页面的数据
        const dataSliced =  database.value.slice(start, end);
        data.value = dataSliced;
        return dataSliced;
    }
    const currentPage = ref(1);
    const currentPageSize = ref(pageSize);
    const total =  computed(() => database.value.length);
    const pageCount = computed(() => Math.ceil(total.value / currentPageSize.value));
    // 监听database变量,当database变量发生变化时,触发sliceData函数,实现分页
    watch([database, currentPage], () => {
        changeData({ currentPage: currentPage.value, currentPageSize: currentPageSize.value });
    });
    // 返回分页后的数据和控制器的数据
    return {
        currentPage,
        currentPageSize,
        total,
        pageCount,
        data,
        goto(val:string|number) {
            currentPage.value = +val;
        },
    };
};

二、页面的分页组件md-pagination中使用

<template>
		....
        <template v-slot:footer>
            <div class="dialog-footer">
                <md-pagination
                    small
                    background
                    layout="total, prev, pager, next"
                    :current-page="pagination.currentPage.value"
                    :page-size="pagination.currentPageSize.value"
                    :total="pagination.total.value|| 0"
                    @current-change="changePage"
                />
               ...
            </div>
        </template>
</template>

<script setup>
import { useUserPagination } from '../../../../../composition/useUserPagination';
//这里省略了接口获取tableData
const pagination = useUserPagination(tableData, 10);
/** 详细地址都加上四级地址 */
const joinAddress = (province, city, district, town, detailAddr) => {
    return (province || '') + (city || '') + (district || '') + (town || '') + (detailAddr || '');
};
const changePage = (val) => {
    pagination.goto(+val);
};

 {
    tableData.value = [];
    info.value = {};
    dialogFormVisible.value = false;
};


</script>
<style lang="less">
.items-info-content {
    padding: 0 10px;
    .title-info {
        margin-bottom: 10px;
        .title {
            width: 70px;
            margin-right: 5px;
        }
    }
    .message-panel {
        flex: 1;
        color: var(--main-color);
        > span {
            margin-right: 10px;
        }
    }
}
.table-list{
    height: 400px;
    overflow: auto;
}
.dialog-footer {
    display: flex;
    flex-direction: row;
    align-content: flex-end;
    justify-content: flex-end;
    align-items: center;
    flex-wrap: nowrap;
    .md-pagination{
        margin-right: 20px;
    }
}
</style>

~~
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值