当前位置: 首页 > news >正文

vue3 element plus el-table封装(二)

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

// BaseTable.vue

<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

<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)
http://www.lryc.cn/news/271565.html

相关文章:

  • cnn lstm结合网络
  • Ubuntu连接xshell
  • nginx安装和配置
  • 【头歌实训】kafka-入门篇
  • 华为云创新中心,引领浙南的数字化腾飞
  • 240101-5步MacOS自带软件无损快速导出iPhone照片
  • github鉴权失败
  • 2023湾区产城创新大会:培育数字化供应链金融新时代
  • 多维时序 | MATLAB实现SSA-GRU麻雀算法优化门控循环单元多变量时间序列预测
  • 二叉树的前序遍历 、二叉树的最大深度、平衡二叉树、二叉树遍历(leetcode)
  • SQL之CASE WHEN用法详解
  • Ubuntu 18.04搭建RISCV和QEMU环境
  • 立足兴趣社交赛道,Soul创新在线社交元宇宙新玩法
  • Couchdb 任意命令执行漏洞(CVE-2017-12636)
  • VectorWorks各版本安装指南
  • 【MySQL】数据库中为什么使用B+树不用B树
  • 微信小程序发送模板消息-详解【有图】
  • Easy Rules规则引擎实战
  • 听GPT 讲Rust源代码--library/alloc(2)
  • OSG读取和添加节点学习
  • 计算机网络技术--念念
  • C#_var
  • Linux---进程控制
  • Java注解学习,一文掌握@Autowired 和 @Resource 注解区别
  • 系列一、如何正确的获取Spring Cloud Alibaba Spring Cloud Spring Boot之间的版本对应关系
  • 数据预处理:标准化和归一化
  • Node.js+Express 路由配置,实现接口分类管理
  • HTML-基础知识-基本结构,注释,文档说明,字符编码(一)
  • 《系统架构设计师教程(第2版)》第3章-信息系统基础知识-05-专家系统(ES)
  • OSCHINA Gitee 联合呈现,《2023 中国开源开发者报告》正式发布,总结分非常帮,可以免费看的报告!