Ant Design Table: Dynamically Merge Cells Based on Duplicate Data
Ant Design doesn't directly provide a feature for dynamically merging cells based on multiple column data. You'll need to implement this functionality yourself. Here's a solution:
-
Define a
getSpanfunction: Create a function namedgetSpan(row, col, data)to determine the number of rows and columns to merge for a given cell based on its row, column, and table data. -
Use the
renderfunction incolumns: Within thecolumnsconfiguration of your table, set arenderfunction for columns that require merging. Inside therenderfunction, call thegetSpanfunction to get the desired merge span values. Additionally, apply styling to the merged cell for visual indication. -
Preprocess Data for Merging: In the
dataSourceof your table, preprocess the data to store information about cells that need to be merged. Use an object likemergedCellsto track this information, with keys representing the cell's row, column, and value, and values containing row/column indices and span information.
Example Code:
import React from 'react';
import { Table } from 'antd';
class App extends React.Component {
getSpan(row, col, data) {
let rowspan = 1;
let colspan = 1;
// Check if current cell value matches the one above
if (row > 0 && data[row][col] === data[row - 1][col]) {
rowspan = 0;
}
// Check if current cell value matches the one to the left
if (col > 0 && data[row][col] === data[row][col - 1]) {
colspan = 0;
}
// Calculate merge span values
for (let i = row + 1; i < data.length; i++) {
if (data[i][col] === data[row][col]) {
rowspan++;
} else {
break;
}
}
for (let j = col + 1; j < data[0].length; j++) {
if (data[row][j] === data[row][col]) {
colspan++;
} else {
break;
}
}
return {
rowspan: rowspan,
colspan: colspan,
};
}
render() {
const dataSource = [
{ name: 'John', age: 18, gender: 'male' },
{ name: 'Lily', age: 20, gender: 'female' },
{ name: 'Lucy', age: 18, gender: 'female' },
{ name: 'Tom', age: 20, gender: 'male' },
{ name: 'Jerry', age: 18, gender: 'male' },
{ name: 'Alice', age: 18, gender: 'female' },
{ name: 'Bob', age: 20, gender: 'male' },
{ name: 'Mary', age: 18, gender: 'female' },
];
const mergedCells = {};
dataSource.forEach((row, rowIndex) => {
Object.keys(row).forEach((col, colIndex) => {
const cellValue = row[col];
if (cellValue !== null && cellValue !== undefined) {
const key = `${rowIndex}-${colIndex}-${cellValue}`;
if (mergedCells[key]) {
mergedCells[key].rowspan++;
mergedCells[key].colspan++;
} else {
mergedCells[key] = {
row: rowIndex,
col: colIndex,
rowspan: 1,
colspan: 1,
};
}
}
});
});
const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
render: (text, record, index) => {
const span = this.getSpan(index, 0, dataSource);
const mergedCell = mergedCells[`${index}-0-${text}`];
return {
children: text,
props: {
rowSpan: span.rowspan || mergedCell.rowspan,
colSpan: span.colspan || mergedCell.colspan,
style: {
background: mergedCell ? '#fafafa' : null,
},
},
};
},
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
render: (text, record, index) => {
const span = this.getSpan(index, 1, dataSource);
const mergedCell = mergedCells[`${index}-1-${text}`];
return {
children: text,
props: {
rowSpan: span.rowspan || mergedCell.rowspan,
colSpan: span.colspan || mergedCell.colspan,
style: {
background: mergedCell ? '#fafafa' : null,
},
},
};
},
},
{
title: 'Gender',
dataIndex: 'gender',
key: 'gender',
render: (text, record, index) => {
const span = this.getSpan(index, 2, dataSource);
const mergedCell = mergedCells[`${index}-2-${text}`];
return {
children: text,
props: {
rowSpan: span.rowspan || mergedCell.rowspan,
colSpan: span.colspan || mergedCell.colspan,
style: {
background: mergedCell ? '#fafafa' : null,
},
},
};
},
},
];
return <Table dataSource={dataSource} columns={columns} />;
}
}
export default App;
In the above code:
- Data is preprocessed to store cell merge information in
mergedCells. - The
renderfunction in each column calculates merge span usinggetSpanandmergedCells. - Merged cells are visually indicated with a light gray background.
- When
getSpanreturns 0, it merges the current cell with the cells above and to the left.
By implementing this approach, you can effectively dynamically merge cells based on duplicate data in your Ant Design table.
原文地址: https://www.cveoy.top/t/topic/nw11 著作权归作者所有。请勿转载和采集!