471 words
2 minutes
Wrapping Antd Table to Auto-Merge Cells with Identical Values in the Same Column

Background#

While building features recently, I kept running into the need to merge cells with identical values in the same column. To make reuse easy, I wrote a small wrapper around the Antd Table component that handles the merging automatically.

Merging cells with identical values in the same column

The Idea#

In an Antd Table column definition, the onCell property lets you set attributes on a cell — so all we need to do is set the right rowSpan for each merged cell.

A quick refresher on rowSpan: it sets how many rows a cell spans. A value of 2 means the cell spans two rows. Every cell that gets absorbed into the merge must have its rowSpan set to 0 so it doesn’t render a duplicate.

Implementation#

import { useMemo } from 'react'
import { Table, TableProps } from 'antd'
export default function SameGroupMergeTable(props: TableProps) {
const { columns, dataSource } = props;
const innerColumns = useMemo(() => {
if (!columns || !dataSource) return [];
return columns.map((column: any) => {
if (column.mergeColumn && !column.onCell) {
return {
...column,
onCell: (record: any, rowIndex: number | undefined) => {
const dataIndex = column.dataIndex;
const sameKey = dataSource.filter((item) => item[dataIndex] === record[dataIndex]);
if (sameKey.length === 1) {
return { rowSpan: 1 };
}
const sameKeyFirstIndex = dataSource.findIndex((item) => item[dataIndex] === record[dataIndex]);
if (rowIndex === sameKeyFirstIndex) {
return { rowSpan: sameKey.length };
}
return { rowSpan: 0 };
},
};
}
// If a column sets both mergeColumn and onCell, we need to rewrite onCell,
// because the caller may rely on other onCell arguments for extra behavior
if (column.mergeColumn && column.onCell) {
return {
...column,
// Rewrite onCell, passing its original arguments plus the merge value (rowSpan) as a third argument
onCell: (record: any, rowIndex: number | undefined) => {
const dataIndex = column.dataIndex;
const sameKey = dataSource.filter((item) => item[dataIndex] === record[dataIndex]);
if (sameKey.length === 1) {
return column.onCell(record, rowIndex, { rowSpan: 1 });
}
const sameKeyFirstIndex = dataSource.findIndex((item) => item[dataIndex] === record[dataIndex]);
if (rowIndex === sameKeyFirstIndex) {
return column.onCell(record, rowIndex, { rowSpan: sameKey.length });
}
return column.onCell(record, rowIndex, { rowSpan: 0 });
},
};
}
return column;
});
}, [columns, dataSource]);
return (
<Table {...props} columns={innerColumns} />
)
}

Usage#

import SameGroupMergeTable from '@/src/components/SameGroupMergeTable';
const columns = [
{
title: 'Name',
dataIndex: 'name',
// Option 1: just set the mergeColumn flag
mergeColumn: true,
},
{
title: 'Name',
dataIndex: 'name',
// Option 2: set mergeColumn together with onCell
mergeColumn: true,
onCell: (_record, rowIndex, mergeObject) => {
if (rowIndex === 0) {
return {
...mergeObject,
style: {
backgroundColor: "#E6F4FF",
}
}
}
return mergeObject;
},
},
];
const dataSource = [];
const Example = () => {
return <SameGroupMergeTable
columns={columns}
dataSource={dataSource}
/>;
};
export default Example;
TIP

Usually setting the mergeColumn flag alone is enough.

If the merged column also needs onCell for other behavior, use the third parameter mergeObject that the onCell function now receives.

Wrapping Antd Table to Auto-Merge Cells with Identical Values in the Same Column
https://www.mihouo.com/en/posts/front/a-wrapper-based-on-the-antd-table-component/
Author
Jia
Published at
2025-09-10
License
CC BY-NC-SA 4.0