Table 数据结构

  • 一条数据默认显示一行,一个 tr

    1. export const dataSource = [
    2. {
    3. type: '黑龙江省',
    4. data: [
    5. {
    6. fault: '10',
    7. faultRate: '11.0%',
    8. successRate: '12',
    9. success: '13',
    10. time: 1623394800000,
    11. total: '14',
    12. cost: 15,
    13. },
    14. {
    15. fault: '20',
    16. faultRate: '21%',
    17. successRate: '22',
    18. success: '23',
    19. time: 1623395300000,
    20. total: '24',
    21. cost: 25,
    22. },
    23. {
    24. faultRate: '30',
    25. faultRate: '31%',
    26. successRate: '32',
    27. success: '33',
    28. time: 1623396700000,
    29. total: '34',
    30. cost: 35,
    31. },
    32. ],
    33. },
    34. {
    35. type: '青海省',
    36. data: [
    37. {
    38. fault: '10',
    39. faultRate: '13%',
    40. successRate: '13%',
    41. success: '15',
    42. time: 1623394800000,
    43. total: '16',
    44. cost: 17,
    45. },
    46. ],
    47. },
    48. ];

动态渲染:时间表头

点击每行,获取当前点击列的数据,思路:

  • 给每个 td添加 对应的时间戳,通过时间戳去匹配数据
  • dataIndex的数组用法 ```jsx import React, { memo } from ‘react’; import PropTypes from ‘prop-types’; import { Table } from ‘antd’;

const children = [ { title: ‘总量’, dataIndex: ‘total’, width: 100, }, { title: ‘成功量’, dataIndex: ‘success’, width: 100, }, { title: ‘成功率’, dataIndex: ‘successRate’, width: 100, }, { title: ‘消耗时间’, dataIndex: ‘cost’, width: 120, }, { title: ‘错误数量’, dataIndex: ‘faultRate’, width: 90, }, ];

function getColumns(dataSource) { const { data = [] } = dataSource[0];

const columns = data.map((item, i) => { const { time } = item; return { title: time, // formatDate(time) children: children.map(child => { const { dataIndex } = child; return { …child, dataIndex: [‘data’, i, dataIndex], key: ${dataIndex}-${i}-${time}, onCell: () => ({ className: time }), }; }), } });

columns.unshift({ title: ‘province’, dataIndex: ‘province’, width: 100, }); return columns; }

TableServiceIndex.propTypes = { value: PropTypes.object.isRequired, onChange: PropTypes.func.isRequired, };

function TableServiceIndex({ value: values, onChange }) { const { loading, dataSource } = values;

function onRow(row) { return { onClick: ev => { const { innerText, className } = ev.target; // const time = ev.target.getAttribute(‘class’); const data = row.data.find(item => item.time === className) onChange({ value: innerText, row: data }); }, } }

const attrs = { rowKey: ‘id’, size: ‘middle’, scroll: { x: 1200 }, loading, columns: getColumns(dataSource), dataSource, bordered: true, onRow, };

return

; }

function propsEqual(prevProps, props) { const { dataSource } = prevProps.value; return dataSource === props.value.dataSource; }

export default memo(TableServiceIndex, propsEqual);

  1. <a name="vlIFX"></a>
  2. ### columns自定义单元格属性
  3. onCell用法:[https://ant.design/components/table-cn/](https://ant.design/components/table-cn/)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/112859/1624432460346-10d7ed7d-b462-47a3-beb3-ab62ab3a5ae9.png#clientId=ufabf5a2f-bb04-4&from=paste&height=38&id=u6c251dd7&originHeight=114&originWidth=1474&originalType=binary&ratio=3&size=14736&status=done&style=none&taskId=u54654f0f-f656-426c-bdc7-2d2241c3bda&width=491.3333333333333)
  4. ```jsx
  5. const columns = [
  6. {
  7. title: '表头',
  8. onCell: (row, index) => {
  9. return {
  10. className: row.time,
  11. };
  12. },
  13. }
  14. ]

dataIndex报错

原因:dataIndex相同,导致的报错,解决,用 key属性解决 dataIndex相同引起的报错
image.png

https://segmentfault.com/q/1010000037701806