父组件
<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>