Ant table表格合并单元格使用

article/2025/9/12 21:49:02

Ant 合并单元格+覆盖样式

项目需求:

最后一列用来展示详情,表格左侧点击行切换;要求选中行的样式和详情信息用一个边框凸显出来

结果如下:

 

切换效果展示如下:

 

实现过程:

在ant合并单元格demo中,找到在线编辑器实现初始版本,实现合并单元格

去除多余的表头和行、列简化项目代码:

const columns = [{title:'name'dataIndex:'name',render:(text) => <a>{text}</a>},{title: 'Age',dataIndex: 'age',},{title:'合并'dataIndex: 'tel',onCell:(_,index)=>{if(index === 0){return {rowSpan:5};}return {rowSpan:0}}},
]

改造了column配置,onCell配置用第一行数据的单元格,平铺占满:效果如下图,

下面是做表格行点击选择,rowClassName属性设置单行class,通过设置这个方法对选中行添加单独的class

onRow方法可以控制表格每行的鼠标交互事件,因此点击选择行的核心代码如下:

​
import React, {useState} from 'react';
import { Table } from 'antd';
​
const App = () =>{
const [selectIndex,setSelectIndex] = useState(0);
return(<Tablecolumns={columns}dataSource={data}pagination={false}onRow={(record,index)=>{return{//点击行onClick:event=>{setSelectIndex(index)}}}}rowClassName={(record,index)=>{if(index === selectIndex) return 'selectedRow';return '';}}/> 
)
}
​

接下来就来到CSS覆盖频道了,实现选中行包围详情背景+边框了:

.ant-table-tbody > :first-child > :last-child {background: #f1f1ff;border: 1px solid red;
}
​
.selectedRow .ant-table-cell {background-color: #f1f1ff;background: #f1f1ff;border: 1px solid red;border-left: none;border-right: none;position: relative;
}
​
.selectedRow>:first-child {border-left: 1px solid red;border-radius: 2px 0 0 2px;
}
​
.selectedRow .ant-table-cell::after {content:' ';position:absolute;width: 4px;height: 54px;background: #f1f1ff;right: -2px;top: 0px;z-index: 999;
}
​
.ant-table-tbody > :first-child > :last-child::after {display: none;
}
​
.selectedRow .ant-table-cell-row-hover {background-color: #f1f1ff !important;background: #f1f1ff !important;
}

上面这些CSS都是常规操作,:1- 核心逻辑是给selectedRow下面的每个单元格加边框、加背景2 -然后通过每个单元格的after伪类覆盖掉不需要的边框

最后是在右侧单元格上显示选中行的详情了,1- 这一步实现思路是直接把columns改造成函数,传入选中行下标作为参数;2- 最后在右侧单元格的render方法中获取到选中行的数据,然后展示详细信息

const columns = (index)=>[{title: 'Name',dataIndex: 'name',render: (text) => <a>{text}</a>,},{title: 'Age',dataIndex: 'age',},{title: 'Home phone',dataIndex: 'tel',onCell: (_, index) => {if (index === 0) {return {rowSpan: 5,};}return {rowSpan: 0};},render:()=>{const current = data[index];return <div><div>name:{current.name}</div><div>age:{current.age}</div><div>phone:{current.phone}</div><div>address:{current.address}</div></div>;}},
];
// 调用时columns传入唯一标识就好了
// <Table columns={columns(selectIndex)} ...
​

这样改造之后,点击就可以显示具体详情了,实际项目中还是要改回 id 之类的唯一健

最后附上demo.js文件,全部代码

demo.js

import React, { useState } from "react";
import "antd/dist/antd.css";
import "./index.css";
import { Table } from "antd";
​
const data = [{key: "1",name: "张三",age: 32,tel: "0571-22098909",phone: 18889898989,address: "New York No. 1 Lake Park"},{key: "2",name: "晓余",tel: "0571-22098333",phone: 18889898888,age: 42,address: "London No. 1 Lake Park"},{key: "3",name: "布布",age: 32,tel: "0575-22098909",phone: 18900010002,address: "Sidney No. 1 Lake Park"},{key: "4",name: "小红",age: 18,tel: "0575-22098909",phone: 18900010002,address: "London No. 2 Lake Park"},{key: "5",name: "小张",age: 18,tel: "0575-22098909",phone: 18900010002,address: "Dublin No. 2 Lake Park"}
];
​
const columns = (index) => [{title: "姓名",dataIndex: "name",render: (text) => <a>{text}</a>},{title: "年龄",dataIndex: "age"},{title: "详情信息",dataIndex: "tel",onCell: (_, index) => {if (index === 0) {return {rowSpan: 5};}return { rowSpan: 0 };},render: () => {const current = data[index];return (<div><div>姓名:{current.name}</div><div>年龄:{current.age}</div><div>联系方式:{current.phone}</div><div>联系地址:{current.address}</div></div>);}}
];
​
const App = () => {const [selectIndex, setSelectIndex] = useState(0);return (<Tablecolumns={columns(selectIndex)}dataSource={data}onRow={(record, index) => {return {// 点击行onClick: (event) => {setSelectIndex(index);}};}}rowClassName={(record, index) => {if (index === selectIndex) return "selectedRow";return "";}}/>);
};
​
export default App;
​

index.css

.ant-table-tbody > :first-child > :last-child {background: #f1f1ff;border: 1px solid red;
}
​
.selectedRow .ant-table-cell {background-color: #f1f1ff;background: #f1f1ff;border: 1px solid red;border-left: none;border-right: none;position: relative;
}
​
.selectedRow > :first-child {border-left: 1px solid red;border-radius: 2px 0 0 2px;
}
​
.selectedRow .ant-table-cell::after {content: " ";position: absolute;width: 4px;height: 54px;background: #f1f1ff;right: -2px;top: 0px;z-index: 999;
}
​
.ant-table-tbody > :first-child > :last-child::after {display: none;
}
​
.selectedRow .ant-table-cell-row-hover {background: #f1f1ff !important;
}
​

index.html

<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="theme-color" content="#000000"></head><body><div id="container" style="padding: 24px" /><script>var mountNode = document.getElementById('container');</script></body>
</html>

index.js

​
import React from 'react';
import ReactDOM from 'react-dom';
import Demo from './demo';
​
ReactDOM.render(<Demo />, document.getElementById('container'));
​

效果图附上:

 

目录

Ant 合并单元格+覆盖样式

实现过程:

demo.js

index.css

index.html

index.js


 


http://chatgpt.dhexx.cn/article/EbTKPlwn.shtml

相关文章

table 表格合并行或列

合并单元格 <table><tbody><tr><th colspan"2">我是占位符</th><th colspan"2">我是占位符</th></tr><tr><th rowspan"2">我是占位符</th><th>我是占位符</th>&…

Element Table 单元格中嵌套表格(Table) 合并行效果

在Element中实现表格合并行功能一般是通过自定义span-method方法&#xff0c;此方法要求表格数据源中需要合并行的单元格中数据相同&#xff0c;根据相同数据来实现合并算法。 本例通过在父Table单元格中嵌套子Table实现合并行效果&#xff08;也使用了span-method方法&#x…

Html Table 合并单元格

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title></title> </head> <body> <ol><li>基本表格</li><table border"2" bordercolor"black" …

使用原生table合并单元格

先上个我要实现的页面 例子1&#xff1a; 就是最近要开发这么一个页面&#xff08;这是个原型图&#xff0c;没有美化&#xff0c;大概是这个样子也都懂&#xff09;&#xff0c;刚看到的时候说实话有点儿懵的&#xff0c;第一次见到这样的表格&#xff0c;可以看到里面有很多…

el-table 合并单元格(合并行)

1. 添加 :span-method"objectSpanMethod" 2. 写objectSpanMethod 方法 //#region 合并单元格// 这个方法是 element-ui提供的单元格合并方法// objectSpanMethod 传入了 { row, column, rowIndex, columnIndex }// row: 当前行// column: 当前列// rowIndex&…

table表格 ---合并单元格

1、合并表头 第一种方法 利用table的 :header-cell-style属性 <el-table:data"tableData"height"400px"max-height"400px"size"small":header-cell-style"headerStyle"fit > methods: {headerStyle({ row, rowIndex …

table表格单元格的合并详解

1.html实现表格 <el-tablemax-height"300":columns"columns":data"tableData":show-index"false":span-method"objectSpanMethod":header-cell-style"{ background: #eef0f6 }" /> 2.记录每个字段合并数的…

table表格--合并单元格

知识点概要&#xff1a; 1、colpan:横向合并"n"个单元格--n:默认1 2、rowspan:纵向合并"n"个单元格--n:默认1 代码demo&#xff1a; <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/lo…

table表格中单元格的合并

目录 table表格中单元格的合并 table表格中单元格的合并很多朋友不一定了解&#xff0c;今天我就写一篇博客来跟大家分享一下table表格中的跨行合并和跨列合并。 我们先看一个合并过的表格&#xff0c;大家可以先思考一下这种表格通过代码是怎么实现的。 解析&#xff1a;我们…

类型“HTMLElement”上不存在属性“getContext”

ts警告&#xff1a;类型“HTMLElement”上不存在属性“getContext” 修改代码如下&#xff1a; const state reactive({canvasDom: null as HTMLCanvasElement | null,canvasCtx: null as CanvasRenderingContext2D | null, });onMounted(() > {state.canvasDom <HTML…

“TypeError: Cannot read properties of null (reading ‘getContext‘)“

目录 一、报错截图 二、使用场景 三、代码截图 四、报错原因 五、解决办法 一、报错截图 二、使用场景 第一次在vue项目种使用canvas&#xff0c;跟着网上教程做&#xff0c;标签canvas写好了&#xff0c;dom元素获取了&#xff0c;简单“画”了一下&#xff0c;运行之后报…

setcontext getcontext makecontext swapcontext

Linux上下文切换以及协程 上下文切换&#xff0c;听起来虚无缥缈&#xff0c;什么是上下文&#xff0c;切换又是指的是什么&#xff1f;其实上下文就可以理解为一个进程所运行的相关的寄存器值&#xff0c;即包括sp/bp/pc等值&#xff0c;换句话说&#xff0c;一个上下文&#…

android之getContext和getActivity介绍

image.png getContext View类中提供的方法&#xff0c;在继承了View的类中才可以调用。 返回的是当前View运行在哪个Activity Contex中&#xff0c;获取当前context的实例。 如果使用场景是Activity则相当于 this&#xff0c;如果使用场景是一个Server 那么获取的实例就是一个A…

sws_getContext函数详细使用

成功后返回SwsContext 类型的结构体。 参数1&#xff1a;被转换源的宽 参数2&#xff1a;被转换源的高 参数3&#xff1a;被转换源的格式&#xff0c;eg&#xff1a;YUV、RGB……&#xff08;枚举格式&#xff0c;也可以直接用枚举的代号表示eg&#xff1a;AV_PIX_FMT_YUV42…

canvas中getContext(“2d“) 对象的属性和方法

HTML5中canvas标签用于绘制图像&#xff08;通过脚本&#xff0c;通常是Js&#xff09;。 也就是说&#xff0c;canvas元素本身没有绘制能力仅仅是图形容 - 您必须使用脚本来完成实际的绘图任务。 getContext() 方法可返回一个对象&#xff0c;该对象提供了用于在画布上绘图的…

Debug:无法找到 getContext() 方法

Mybatis反向生成Swagger自动注释 - SegmentFault 思否 在使用该插件时 无法找到 getContext() 方法&#xff0c;是因为 mybatis-generator-core版本高于1.4.0&#xff0c;建议使用1.4.0一下版本进行构建

Android Context解析以及getContext()、getApplication()、getApplicationContext()和getBaseContext()区别

文章目录 Context 介绍Context数量getContext()、getApplication()、getApplicationContext()和getBaseContext()区别getContextgetApplication()、getApplicationContext()getBaseContext() Context 介绍 Android程序不像Java程序一样&#xff0c;随便创建一个类&#xff0c;…

sws_getContext和sws_scale分析

struct SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param);创建转换上下文&#xff0c;参数分析&#xff1a;…

View Fragment Window 的 getContext() 一定返回 Activity 吗?

目录 1. 问题分析 1.1 Context 有哪些&#xff1f; 首先&#xff0c;我们回顾一下 Context 以及它的子类&#xff0c;在之前的这篇文章里&#xff0c;我们曾经讨论过&#xff1a;《Android | 一个进程有多少个 Context 对象&#xff08;答对的不多&#xff09;》。简单来说&a…

HTML5 canvas 参考手册

HTML5 <canvas> 参考手册 描述 HTML5 <canvas> 标签用于绘制图像&#xff08;通过脚本&#xff0c;通常是 JavaScript&#xff09;。 不过&#xff0c;<canvas> 元素本身并没有绘制能力&#xff08;它仅仅是图形的容器&#xff09; - 您必须使用脚本来完成实…