封装原生html的table处理方法【参数类似eltable】

embedded/2024/9/23 6:32:27/

直接跑html即可

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>封装原生talbe</title>
</head>
<body><div id="table_content"></div>
</body><script>// ---------------------------------------------------------封装开始------------------// 把vNode对象转为html字符串function vnodeToString(vnode) {// 如果是文本节点,直接返回文本内容if (['string', 'boolean', 'undefined', 'null', 'number'].includes(typeof vnode) || !vnode) {return vnode;}// 转换节点的属性为字符串形式const attrs = Object.keys(vnode.attrs || {}).map((key) => `${key}="${vnode.attrs[key]}"`).join(' ');// 转换子节点为字符串形式const children = (vnode.children || []).map(vnodeToString).join('');// 返回包含标签名、属性和子节点的字符串形式return `<${vnode.tag} ${attrs}>${children}</${vnode.tag}>`;}class DataToExcelHtml {// 原始数据originalData = []// 表格列配置数据columnsData = []// 渲染数据vNoderenderData = []// 表头vNode数据headerVNode = []// 表体vNode数据bodyVNode = []// 列宽colWidth = 120constructor(config) {this.dom = config.domthis.colWidth = config.colWidth || 120this.originalData = config.datathis.columnsData = config.columnsDatathis._setColIndex()this.headerVNode = this._setHeaderVNode()this.bodyVNode = this._setBodyVNode()this.setInnerHtml()}// 取最后一层flattenObjectArrayLast(arr, key = "children") {let flattened = [];arr.forEach(v => {if (v.children && v.children.length > 0) {flattened = flattened.concat(this.flattenObjectArrayLast(v[key]))} else {flattened.push(v);}})return flattened;}// 表头   设置每个字段所在的列 行下标值_setColIndex(data = this.columnsData, index = 0, row_index = 0) {data.forEach(v => {v.__colspan = this.flattenObjectArrayLast(v.children || []).length || 1v.__rowspan = v.rowspan || 1v.__col_index = v.__colspan > 1 ? null : indexv.__row_index = row_indexindex++if (v.children?.length) {index = this._setColIndex(v.children, index, row_index + 1)}})return index}// 设置样式_setStyle(col) {// 表样式通用const commonTrStyle = "height: 30px;"const commonBorder = 'border-width:1px;border-style:solid;border-color:#000000;'const commonAttrsLabel = {style: `text-align:${col.align || 'center'};font-size: 12px;` + commonBorder + commonTrStyle + col.styleStr,}return commonAttrsLabel}// 设置表头_setHeaderVNode(data = this.columnsData) {const that = this// 递归获取表头合并行深度let deep = (function getDeep(list) {let deep = 1list.forEach(col => {let curDeep = 1if (col.children && col.children.length) {curDeep += getDeep(col.children)}deep = curDeep > deep ? curDeep : deep})return deep})(data)// 递归获取表头VNodethis.headerVNode = (function recData(list, curDeep = 1, tr = []) {list.forEach(col => {tr[col.__row_index] = tr[col.__row_index] || { tag: 'tr', children: [] }const obj = {tag: 'td',children: [col.label],attrs: {style: that._setStyle(col).style,rowspan: col.__rowspan || 1,colspan: col.__colspan || 1,width: col.width || that.colWidth,}}if (col.children) {recData(col.children, curDeep++, tr)} else if (!col.children?.length) {}tr[col.__row_index].children.push(obj)})return tr})(data)return this.headerVNode}_setBodyVNode(data = this.originalData, columnsData = this.columnsData) {const flatColumns = this.flattenObjectArrayLast(columnsData).filter(v => v.__col_index !== null)function getChild(row, col, index) {if (col.type === 'index') {return index + 1}return row[col.prop] || ''}this.bodyVNode = data.map((row, index) => {const tr = { tag: 'tr', children: [] }// 列下标flatColumns.forEach((col, idx) => {tr.children.push({tag: 'td',children: [getChild(row, col, index)],attrs: {style: this._setStyle(col).style,}})})return tr})return this.bodyVNode}// 设为innerHtmlsetInnerHtml(vNode = this.headerVNode) {const i = vnodeToString({tag: 'table',attrs: {style: "border-collapse: collapse;border:1px",border: 1,id: 'excel-line-data'},children: [{tag: 'tbody',attrs: {style: ""},children: [].concat(vNode, this.bodyVNode)}]})this.dom.innerHTML = i}}// ---------------------------------------------------------封装结束------------------// ---------------------------------------------------------调用new DataToExcelHtml({dom: document.querySelector('#table_content'),data: [{ tlj: '测试1', time: '2023年', status: '001' },{ tlj: '测试2', time: '2023年', status: '001' },{ tlj: '测试3', time: '2023年', status: '001' },{ tlj: '测试4', time: '2023年', status: '001' },],columnsData: [{label: '标题',styleStr: 'color: red; font-size: 30px; font-weight: 700',children: [{label: 'xxx时间',align: 'left',children: [{label: "序号",type: 'index',rowspan: 2,},{label: "姓名",prop: 'tlj',width: 300,rowspan: 2,},{label: "---",children: [{label: "时间",width: 300,prop: "time"},{label: "状态",prop: "status",},]},]}]}]})</script><style>td {box-sizing: border-box;white-space: nowrap;min-width: 100px;}table {table-layout: fixed;}td {padding: 0 10px;}#table_content {overflow-x: auto;}
</style></html>


http://www.ppmy.cn/embedded/7271.html

相关文章

【贪玩巴斯】Vm/ubantu虚拟机报错04005和70057解决方法如下(基于WindowsX64系统)

&#x1f4da;Vm/ubantu虚拟机报错04005和70057解决方法如下&#xff08;基于WindowsX64系统&#xff09;&#xff1a; 错误1场景描述&#xff1a;04005 返回 代码:E_FAIL (0X80004005)组件:SessionMachine界面:ISession {c0447716-ff5a-4795-b57a-ecd5fffa18a4}打开电脑启动虚…

mapreduce中的ReduceTask工作机制(Hadoop)

ReduceTask 是 Hadoop 中的一个重要组件&#xff0c;负责对 MapTask 的输出进行合并、排序和归并&#xff0c;最终生成最终的输出结果。 ReduceTask 的工作机制 1. 分组&#xff08;Shuffle&#xff09;阶段&#xff1a; 在分组阶段&#xff0c;ReduceTask 会从多个 Mapper …

Spark---核心概念(Spark,RDD,Spark的核心构成组件)详解

一、什么是Spark Spark就是一个集成离线计算&#xff0c;实时计算&#xff0c;SQL查询&#xff0c;机器学习&#xff0c;图计算为一体的通用的计算框架。 二、Spark特点 1、速度快 相比较于MR&#xff0c;官方说&#xff0c;基于内存计算spark要快mr100倍&#xff0c;基于磁…

在线拍卖系统,基于SpringBoot+Vue+MySql开发的在线拍卖系统设计和实现

目录 一. 系统介绍 二. 功能模块 2.1. 管理员功能模块 2.2. 用户功能模块 2.3. 前台首页功能模块 2.4. 部分代码实现 一. 系统介绍 随着社会的发展&#xff0c;社会的各行各业都在利用信息化时代的优势。计算机的优势和普及使得各种信息系统的开发成为必需。 在线拍卖系…

centos 6设置yum源遇到的问题

由于centos6已经不被支持了&#xff0c;直接抄人家的命令是不行的 比如执行这些&#xff08;是wget或者是curl按照自己的改&#xff09; wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo yum makecache会报错 需要到对应的镜像源网…

win docker clickhouse 挂载本地目录到容器后无法写入数据问题解决

win docker 部署clickhouse 挂载本地目录到容器后无法写入数据问题具体错误提示代码尝试在docker compose 文件中添加文件操作的许可进一步在docker compose 中配置 ulimits参数如下修改映射的本地目录到d盘最后使用docker的数据卷来映射到容器内部目录&#xff0c;解决了不能…

《6G数据面架构研究》

目录 一、数据服务的定义二、6G数据服务驱动力及面临的挑战6G数据服务的业务驱动6G数据服务的技术驱动6G数据服务的网络内在驱动6G数据面面临的挑战 三、6G数据服务典型场景自动化网络运维用户体验提升通信感知数据服务 四、6G数据面架构研究数据面架构视图功能定义说明&#x…

golang rabbitmq客户端连接及重连

目录 1、连接、发送、发送异常、重连2、调用示例 1、连接、发送、发送异常、重连 package rabbitmqimport ("encoding/json""fmt""time""github.com/sirupsen/logrus""github.com/streadway/amqp" )type RabbitMQ struct {…