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)
相关推荐
weifont2 小时前
聊一聊Electron中Chromium多进程架构
javascript·架构·electron
大得3692 小时前
electron结合vue,直接访问静态文件如何跳转访问路径
javascript·vue.js·electron
it_remember4 小时前
新建一个reactnative 0.72.0的项目
javascript·react native·react.js
敲代码的小吉米5 小时前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
是千千千熠啊5 小时前
vue使用Fabric和pdfjs完成合同签章及批注
前端·vue.js
da-peng-song5 小时前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis
九月TTS5 小时前
TTS-Web-Vue系列:组件逻辑分离与模块化重构
前端·vue.js·重构
低代码布道师6 小时前
第五部分:第一节 - Node.js 简介与环境:让 JavaScript 走进厨房
开发语言·javascript·node.js
满怀10157 小时前
【Vue 3全栈实战】从响应式原理到企业级架构设计
前端·javascript·vue.js·vue
梅子酱~7 小时前
Vue 学习随笔系列二十三 -- el-date-picker 组件
前端·vue.js·学习