基于elementplus表格的二次封装

父组件

<template>
    <!-- <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="名字" />
        <el-table-column prop="address" label="地址" />
    </el-table> -->
    <AntTable :data="tableData" :cloumns="cloumns" 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 tableData = [
    {
        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',
    },
];
const cloumns = ([
    {
        label: '名字',
        key: 'name',
    },
    {
        label: '地址',
        key: 'address',
    },
    {
        label: '操作',
        width: '100px',
        key: 'console',
    },
]);
const handleClick = (key) => {
    console.log(key);
};
</script>

子组件 AntTable.vue

<template>
    <el-table v-bind="$attrs">
        <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>
</template>

<script setup lang="ts">
import { withDefaults } from 'vue';
const props = withDefaults(defineProps<{
    // data: { [props: string]: any }[],
    cloumns: { [props: string]: any }[],
}>(), {})
</script>

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值