vue3 element plus el-table封装(二)

上文是对el-table的基本封装,只能满足最简单的应用,本文主要是在上文的基础上增加slot插槽,并且对col插槽进行拓展,增加通用性

// BaseTable.vue

js 复制代码
<template>
  <el-table>
    <template v-for="name in tableSlots" :key="name" #[name]>
      <slot :name="name"></slot>
    </template>
    <el-table-column v-for="(col,index) in $attrs.config" :key="index" v-bind="col">
      <template v-if="col.slot" #[getColSlot(col)]="scope" >
        <slot :name="col.slot" v-bind="scope"></slot>
      </template>
    </el-table-column>
  </el-table>
</template>
<script lang="ts" setup>
const slots=useSlots();
const tableSlots=computed(()=>{
  // 原生el-table插槽只有default,append,empty
  // 原生el-table-column插槽只有table插槽只有default,header
  // 这里将header单独处理,认为是col的插槽,从table插槽中排除
  return Object.keys(slots).filter(name=>name!=='header')
})
const getColSlot=(col)=>{
  return col.slot==='header'?'header':'default'
}
</script>

//index.vue

js 复制代码
<template>
  <BaseTable :config="config" :data="tableData" :style="{width:'800px'}">
    <template #status="scope">
      <el-text :type="scope.row.status.type ">{{ scope.row.status.content }}</el-text>
    </template>
    <template #btn="scope">
      <el-button type="primary">{{ scope.row.btn }}</el-button>
    </template>
    <!-- 如有多个类似slot,也可以用如下方式渲染 -->
    <!-- <template  v-for="(col,index) in config.filter(item=>item.slot && item.slot!=='header')" :key="index" #[col.slot]="scope">
      <el-button type="primary">{{ scope.row.btn }}</el-button>
    </template> -->
    <template #header="scope">
      <el-button type="primary">{{ scope.column.label }}</el-button>
    </template>
  </BaseTable>
</template>

<script lang="ts" setup>
import BaseTable from './BaseTable.vue'
const config=[
  {
    type:'selection'
  },
  {
    prop: 'date',
    label:'日期',
    width:'180'
  },
  {
    prop: 'name',
    label:'姓名',
  },
  {
    prop: 'status',
    label:'状态',
    slot:'status',
    width:'180'
  },
  {
    prop: 'btn',
    label:'操作',
    slot:'btn',
    width:'180'
  },
  {
    prop: 'header',
    label:'按钮header',
    slot:'header',
    width:'180'
  },
];
const tableData = [
  {
    date: '2016-05-03',
    name: '张三',
    status:{content:'工作',type:'success'},
    btn: 'confirm',
  },
  {
    date: '2016-05-02',
    name: '李四',
    status:{content:'出差',type:'primary'},
    btn: 'confirm',
  },
  {
    date: '2016-05-04',
    name: '王五',
    status:{content:'休假',type:'danger'},
    btn: 'confirm',
  },
]
</script>
复制代码
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/dad5e7be52c84c12997da0829cdf9178.png)
相关推荐
老华带你飞20 分钟前
记录生活系统|记录美好|健康管理|基于java+Android+微信小程序的记录生活系统设计与实现(源码+数据库+文档)
android·java·数据库·vue.js·生活·毕设·记录生活系统
涔溪26 分钟前
vue中预览pdf文件
前端·vue.js·pdf
孤狼warrior27 分钟前
目前最新同花顺金融股市数据爬取 JS逆向+node.js补浏览器环境
javascript·爬虫·python·金融·node.js
一 乐1 小时前
海产品销售系统|海鲜商城购物|基于SprinBoot+vue的海鲜商城系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·后端
艾小码1 小时前
还在死磕模板语法?Vue渲染函数+JSX让你开发效率翻倍!
前端·javascript·vue.js
炒毛豆1 小时前
vue3 + antd + print-js 实现打印功能(含输出PDF)
前端·javascript·vue.js
天天向上10241 小时前
el-table动态添加行,删除行
前端·javascript·vue.js
小王码农记2 小时前
vue2中实现天气预报
前端·javascript·vue.js·echarts
我命由我123452 小时前
Element Plus 组件库 - Select 选择器 value 为 index 时的一些问题
开发语言·前端·javascript·vue.js·html·ecmascript·js
一念一花一世界2 小时前
Arbess从初级到进阶(2) - 使用Arbess+GitLab实现Vue.js项目自动化部署
vue.js·ci/cd·gitlab·arbess