基于elementplus表格+分页的二次封装

父组件

<template>

    <AntTable :data="data" :cloumns="cloumns" :pagination="{ pageSize: 5 }" border stripe style="width: 800px;">
        <template #console="scope">
            <el-button @click="handleClick(scope.row.key)">Detail</el-button>
        </template>
        <template #address="scope">
            <el-tag>{{ scope.row.address }}</el-tag>
        </template>
    </AntTable>
</template>
  
<script  setup>
const data = [
    {
        key: 1,
        name: 'Tom',
        address: 'No. 189, Grove St, Los Angeles',
    },
    {
        key: 2,
        name: 'Tom',
        address: 'No. 189, Grove St, Los Angeles',
    },
    {
        key: 3,
        name: 'Tom',
        address: 'No. 189, Grove St, Los Angeles',
    },
    {
        key: 4,
        name: 'Tom',
        address: 'No. 189, Grove St, Los Angeles',
    },
    {
        key: 4,
        name: 'Tom',
        address: 'No. 189, Grove St, Los Angeles',
    },
    {
        key: 4,
        name: 'Tom',
        address: 'No. 189, Grove St, Los Angeles',
    },
    {
        key: 4,
        name: 'Tom',
        address: 'No. 189, Grove St, Los Angeles',
    },
    {
        key: 4,
        name: 'Tom',
        address: 'No. 189, Grove St, Los Angeles',
    }
];
const cloumns = ([
    {
        label: '名字',
        key: 'name',
    },
    {
        label: '地址',
        key: 'address',
    },
    {
        label: '操作',
        width: '100px',
        key: 'console',
    },
]);
const handleClick = (key) => {
    console.log(key);
};
</script>

子组件

<template>
    <el-table v-bind="$attrs" :data="tableData">
        <el-table-column v-for="v in cloumns" :prop="v.key" :label="v.label" v-bind="v">
            <template #default="scope" v-if="$slots[v.key]">
                <slot :name="v.key" v-bind="scope"></slot>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination v-if="$attrs.pagination" :current-page="current" @current-change="handleCurrentChange"
        @prev-click="handlePrevPage" @next-click="handleNextPage" layout="total ,prev, pager, next" :total="data.length"
        v-bind="$attrs.pagination" />
</template>

<script setup lang="ts">
import { withDefaults, ref, computed, useAttrs } from 'vue';
const attrs = useAttrs();
const props = withDefaults(defineProps<{
    data: { [props: string]: any }[],
    cloumns: { [props: string]: any }[],
}>(), {})
//当前页  默认第一页
const current = ref(1);
//当前页码发生改变的事件
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`)
    current.value = val;
}
const handlePrevPage = (val: number) => {
    console.log(`${val}`)
}
const handleNextPage = (val: number) => {
    console.log(`${val}`)
}
const tableData = computed(() => {
    if (attrs.pagination) {
        return props.data.slice(
            (current.value - 1) * attrs.pagination.pageSize,
            current.value * attrs.pagination.pageSize
        )
    } else {
        return props.data
    }
})
/*
页码推到公式
pageSize  一夜显示多少条  currentPage  当前页码  total 总数
    第一页 1:  (0,5)
    第二页 2:   (5,10)
    第三页 3:   (10,15)
    第n页:      ( (n-1)*pageSize,n*pageSize )

*/
</script>

<style lang="scss" scoped></style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值