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:

  1. Define a getSpan function: Create a function named getSpan(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.

  2. Use the render function in columns: Within the columns configuration of your table, set a render function for columns that require merging. Inside the render function, call the getSpan function to get the desired merge span values. Additionally, apply styling to the merged cell for visual indication.

  3. Preprocess Data for Merging: In the dataSource of your table, preprocess the data to store information about cells that need to be merged. Use an object like mergedCells to 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 render function in each column calculates merge span using getSpan and mergedCells.
  • Merged cells are visually indicated with a light gray background.
  • When getSpan returns 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.

Ant Design Table: Dynamically Merge Cells Based on Duplicate Data

原文地址: https://www.cveoy.top/t/topic/nw11 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录