You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DevExpress React Grid中实现首行单元格合并且其余行不变?

DevExpress React Grid 第一行单元格合并实现方案

修改后的单元格组件代码

const Cell = props => {
  const { tableColumn, rowIndex } = props;
  // 仅处理第一行的单元格合并
  if (rowIndex === 0) {
    if (tableColumn.column.name === "name") {
      // 合并4列,需与表格实际列数对应
      return <Table.Cell {...props} colSpan={4} />;
    }
    // 第一行的其他列不渲染
    return null;
  }
  // 非第一行正常渲染所有单元格
  return <Table.Cell {...props} />;
};

关键说明

  • 通过rowIndex判断当前行是否为第一行,仅对第一行执行合并逻辑,完全不影响其他行的原有结构与样式
  • 第一行仅保留目标列(示例中为name列)的单元格,设置colSpan值为表格总列数(示例为4)实现跨列合并
  • 非第一行直接返回默认的Table.Cell组件,保持表格原有渲染逻辑

完整渲染代码

render() {
  const { rows, columns } = this.state;

  return (
    <Grid rows={rows} columns={columns}>
      <Table cellComponent={Cell} />
      <TableHeaderRow />
    </Grid>
  );
}

内容的提问来源于stack exchange,提问作者Lynn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 05:42:41