vue之render和slot-scope自定义表格table

各大前端组件库中都存在table组件,均支持slot插槽和render渲染,现在就试着探究其中原理:

基本列表

首先制定表格API。表格分为表头thead和数据tbody,所以props也定义为两部分:

  • columns: 列配置Array,每一列均为Object:
    - title: 列文字
    - key: 对应列内容字段名称
    - render: 渲染函数
    - slot:插槽名称
  • data: 数据内容Array
    在components目录中新建table目录,创建table.vue文件:
// components/table
<template>
    <table>
        <thead>
            <tr>
                <th v-for="(col, index) in columns" :key="index">
                    {
   {
    col.title }}
                </th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="(row, index) in data" :key="index">
                <td v-for="(col, idx) in columns" :key="idx">
                    {
   {
    row[col.key] }}
                </td>
            </tr>
        </tbody>
    </table>
</template>
<script>
export default {
   
    props: {
   
        columns: {
   
            type: Array,
            default: () => []
        },
        data: {
   
            type: Array,
            default: () => []
        },
    }
};
</script>
<style>
table {
   
    width: 100%;
    border
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值