logo

React Antd Table 实现单元格自动合并

作者:狼烟四起2024.01.18 11:10浏览量:8

简介:本文介绍了如何在 React Antd Table 中实现单元格自动合并,通过 colSpan 和 rowSpan 属性可以实现行列合并,满足不同场景下的表格展示需求。

在 React Antd Table 中,我们可以使用 colSpan 和 rowSpan 属性来实现单元格的自动合并。通过指定 colSpan 和 rowSpan 的值,可以控制单元格跨越的行数和列数,从而实现行列合并的效果。
下面是一个简单的示例,展示如何使用 colSpan 和 rowSpan 实现单元格的合并:

  1. import React from 'react';
  2. import { Table } from 'antd';
  3. const data = [
  4. {
  5. key: '1',
  6. name: '张三',
  7. age: 32,
  8. address: '北京市朝阳区',
  9. },
  10. {
  11. key: '2',
  12. name: '李四',
  13. age: 42,
  14. address: '北京市海淀区',
  15. },
  16. {
  17. key: '3',
  18. name: '王五',
  19. age: 32,
  20. address: '北京市朝阳区',
  21. },
  22. ];
  23. const columns = [
  24. {
  25. title: '姓名',
  26. dataIndex: 'name',
  27. key: 'name',
  28. render: (text, row, index) => {
  29. // 根据条件判断是否需要合并单元格
  30. if (index === 0) {
  31. return <b>{text}</b>; // 合并第一行的单元格,添加样式
  32. } else {
  33. return text; // 其他行正常显示
  34. }
  35. },
  36. },
  37. {
  38. title: '年龄',
  39. dataIndex: 'age',
  40. key: 'age',
  41. },
  42. {
  43. title: '地址',
  44. dataIndex: 'address',
  45. key: 'address',
  46. },
  47. ];
  48. const MyTable = () => (
  49. <Table columns={columns} dataSource={data} />
  50. );

相关文章推荐

发表评论