vue3 elementUi table自由渲染组件

ops/2025/3/16 0:45:06/

文章目录

    • 前言
    • CustomTable
    • 如何使用
    • tableColumn 属性
    • h函数
      • 创建原生元素
      • 创建组件
      • 动态生成

前言

elementui中的table组件,表格中想要自由地渲染内容,是一种比较麻烦的事情,比如你表格中想要某一列插入一个button按钮,是不是要用插槽,那下一次某一列插入一个图片,又得新开一种插槽或者类别。

那么,有没有什么方法,能够通过配置,直接配置一个组件的方式,让表格的那一列直接渲染对应的组件。elementui table中并不提供这样的配置。所以需要开发人员自己封装。

CustomTable

在element UI 中Table组件的基础上,封装一个可以自定义渲染的table

<template><!-- 表格 --><el-table:data="tableData"v-loading="loading":empty-text="'暂无数据'"v-bind="$attrs"><!-- :border="true" --><template v-for="column in tableColumn" :key="column?.label"><el-table-column v-if="column.cols" v-bind="{ ...column }"><!-- 列名合并,存在cols的情况下 --><template v-for="col in column.cols" :key="column?.label + col?.label"><el-table-column v-bind="{ ...col }"><template #default="scope"><!-- 自定义render --><span v-if="col?.render" :class="col?.class" :style="col?.style ? col.style : {}">{{ col?.render(scope.row[col.prop], scope.row) }}</span><!-- 自定义render component 将当前列的数据和这一整行的数据都传给component函数 --><div v-else-if="col?.component" v-html="renderVNode(col.component(scope.row[col.prop], scope.row))"> </div><spanv-else:style="col?.style ? col.style : {}":class="col?.class":title="scope.row[col.prop]":data-message="scope.row[col.prop]">{{ scope.row[col.prop] }}</span></template></el-table-column></template></el-table-column><el-table-column v-else v-bind="{ ...column }"><template #default="scope"><!-- 自定义render字符串 当前列的数据和这一整行的数据都传给render函数--><span v-if="column?.render" :style="column?.style ? column.style : {}" :class="column?.class">{{ column?.render(scope.row[column.prop], scope.row) }}</span><!-- 自定义render component 将当前列的数据和这一整行的数据都传给component函数 --><div v-else-if="column?.component" v-html="renderVNode(column.component(scope.row[column.prop], scope.row))"></div><spanv-else:style="column?.style ? column.style : {}":class="column?.class":title="scope.row[column.prop]":data-message="scope.row[column.prop]">{{ scope.row[column.prop] }}</span></template></el-table-column></template></el-table>
</template><script lang="ts" setup name="customTable">const props = defineProps({tableData: {type: Array,default: () => [],},tableColumn: {type: Array<any>,default: () => [],},loading: {type: Boolean,default: false,},});const generateRandomString = (length) => {const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';const charactersLength = characters.length;let result = '';for (let i = 0; i < length; i++) {const randomIndex = Math.floor(Math.random() * charactersLength);result += characters.charAt(randomIndex);}return result;};
// 通过renderVNode 接收一个vnode节点来渲染component
const renderVNode = (vnode: VNode) => {const tempDiv = document.createElement('div');const parentDiv = document.createElement('div');const id = generateRandomString(10);parentDiv.appendChild(tempDiv);tempDiv.id = id;// 利用createApp和render将vnode挂载成一个新的appconst Comp = createApp({render: () => vnode,});// 将这个新的vue app 挂载到对应的dom上nextTick(() => {// 但要放置到nextTick里面,以防止dom还没放置到table的对应位置,就先挂在了Comp.mount('#' + id);});// 返回一个html dom ,利用v-html挂载到table中对应的位置return parentDiv.innerHTML;};
</script >

如何使用

<template><custom-table maxHeight="100vh" :tableColumn="tableColumn" :tableData="tableData" />
</template><script lang="ts" setup name="test">
import componentTest from '../componentTest.vue';
import customTable from '../components/customTable.vue';
const tableColumn = ref([{label: '名称',prop: 'name',align: 'center',minWidth: 130,fixed: true,component: (value, row) => {return h('div', { onClick: () => handleClick(row?.name) }, [h('span', { style: { fontSize: '14px', display: 'inline-block' } }, value),h('p', { class: 'dealer-scale' }, row?.scale),]);},},{label: '订单',prop: 'order',component: (value, row) => {return h(componentTest, {title: value})},style: { color: '#C00017' },},{ label: '本年', prop: 'open', style: { color: '#C00017' } },{ label: '费效比', prop: 'efficiency', style: { color: '#038810' },render: (value,rows) => {console.log(value, rows)return value + '%'}},{ label: '处罚', prop: 'punish', style: { color: '#0000aa' } },]);const tableData = ref([// name order open efficiency punish scale{name: 'xxxx有限公司',order: '23455 万',open: '234 万瓶',efficiency: '1.3 %',punish: '3 次',scale: '10亿以上',},{name: '软件有限公司',order: '23456 万',open: '234 万瓶',efficiency: '1.3 %',punish: '3 次',scale: '5千万~1亿',},]);
</script>

tableColumn__181">tableColumn 属性

<table>属性名类型解释stylestring能够单独配置某一列的样式classstring能够单独配置某一列的class名称renderfunction接受两个参数 (value, row) value是当前行当前列的数据,row是当前行的数据 返回的值会渲染在table对应的列中componentfunction接受两个参数 (value, row) value是当前行当前列的数据,row是当前行的数据,返回一个vnode,通过vue3自带的h函数实现fixedboolean是否让当且列固定………剩下的属性…查看element ui 中table 的Column配置 https://element.eleme.io/#/zh-CN/component/table#table-column-attributestable>

h函数

function h(type: string | Component, // 元素/组件类型props?: object | null,    // 属性/Propschildren?: VNodeChildren  // 子节点(字符串、数组、插槽等)
): VNode

创建原生元素

import { h } from 'vue'// 等价于 <div class="box" id="app">Hello</div>
h('div', { class: 'box', id: 'app' }, 'Hello')// 带事件
h('button', { onClick: () => console.log('Clicked!') }, 'Click me')

创建组件

import MyComponent from './MyComponent.vue'// 传递 props 和插槽
h(MyComponent, { title: 'Demo',onClick: () => {} // 监听自定义事件
}, {default: () => h('span', '默认插槽'),footer: () => h('div', 'Footer 内容')
})

动态生成

const items = ['A', 'B', 'C']
h('ul', items.map(item => h('li', item))
)

http://www.ppmy.cn/ops/166076.html

相关文章

CSS元素层叠顺序规则

CSS元素层叠顺序规则 看图说话总结: background/borderz-index(<0)blockfloatinline/inline-blockz-index(0,auto)z-index (>0)

【CentOS】搭建Radius服务器

目录 背景简介&#xff1a;Radius是什么&#xff1f;Radius服务器验证原理搭建Radius服务器环境信息yum在线安装配置FreeRADIUS相关文件clients.conf文件users文件重启服务 验证 参考链接 背景 在项目中需要用到Radius服务器作为数据库代理用户的外部验证服务器&#xff0c;做…

基于微信小程序的小区管理系统设计与实现【lw+源码+部署+视频+讲解】

第1章 绪论 1.1 研究背景 互联网时代不仅仅是通过各种各样的电脑进行网络连接的时代&#xff0c;也包含了移动终端连接互联网进行复杂处理的一些事情。传统的互联网时代一般泛指就是PC端&#xff0c;也就是电脑互联网时代&#xff0c;但是最近几十年&#xff0c;是移动互联网…

数据挖掘导论——第二章:数据

谈数据之前&#xff0c;我们要先知道数据有哪几种类型。数据的维度&#xff0c;数据的频率、位置、分布&#xff08;方差或标准差衡量&#xff09;等。 接着就是数据的质量&#xff0c;数据挖掘着眼于要么是对数据质量问题的检测和纠正&#xff0c;要么是使用可以容忍低质量数…

c#使用redis如何实现数据的分库存储

在 C# 中使用 Redis 实现数据的分库存储,可以通过以下几种方案实现。以下详细说明并提供代码示例: 方案 1:Redis 多数据库索引(逻辑分库) Redis 默认支持 0-15 共 16 个逻辑数据库,通过索引切换。适用于简单场景。 步骤 连接时指定数据库索引: using StackExchange.Re…

PGSQL基本使用

PGSQL基本使用 文章目录 PGSQL基本使用日期转换长度不够补数获取上下行取连续的开始和结束的值 日期转换 格式说明YYYY年MM月DD日hh2424小时制mi分钟ss秒 -- 日期字符串转指定日期字符串 -- 20250101123000 转为 2025-01-01 12:30:00 select to_char(to_timestamp(2025010112…

Android Studio搭建环境并运行项目

参考&#xff1a; android studio开发环境搭建全过程_androidstudio 搭建开发环境-CSDN博客 Android Studio 开发环境搭建与项目结构认识 1、下载Android Studio 和Java JDK 并配置 &#xff08;安装教程看百度&#xff09;&#xff0c;我这里使用的是Android Studio 2024.01版…

【零基础入门unity游戏开发——进阶篇】Marhf和Math的使用

考虑到每个人基础可能不一样,且并不是所有人都有同时做2D、3D开发的需求,所以我把 【零基础入门unity游戏开发】 分为成了C#篇、unity通用篇、unity3D篇、unity2D篇。 【C#篇】:主要讲解C#的基础语法,包括变量、数据类型、运算符、流程控制、面向对象等,适合没有编程基础的…