index.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, { useMemo, useCallback } from 'react';
  2. import { Table } from '@douyinfe/semi-ui';
  3. import bigJson from '../data/big.json';
  4. const Demo = () => {
  5. const dataTotalSize = 200;
  6. const childrenRecordName = 'children';
  7. const rowKey = 'key';
  8. const authPoint = 'auth_point_list';
  9. const checkName = 'role_perm';
  10. const columns = [
  11. {
  12. title: 'NAME',
  13. dataIndex: 'en_name',
  14. width: 250,
  15. fixed: 'left',
  16. key: 'en_name',
  17. render: (text, record, index, { expandIcon: realExpandIcon }) => {
  18. return (
  19. <>
  20. {/* {record.description ? realExpandIcon : null} */}
  21. {text}
  22. </>
  23. );
  24. },
  25. },
  26. { title: 'APP_ID', dataIndex: 'app_id', key: 'app_id', width: 200 },
  27. { title: 'RESOURCE_POINT_CODE', dataIndex: 'resource_point_code', key: 'resource_point_code' },
  28. {
  29. width: 200,
  30. title: 'Action',
  31. dataIndex: '',
  32. key: 'x0',
  33. render: () => <a>Delete</a>,
  34. fixed: 'right',
  35. },
  36. {
  37. width: 200,
  38. title: 'Action',
  39. dataIndex: '',
  40. key: 'x1',
  41. render: () => <a>Delete</a>,
  42. fixed: 'right',
  43. },
  44. ];
  45. const scroll = {
  46. // x: columns.reduce((prev, col) => (col.width ? col.width + prev : prev), 200),
  47. x: 1200,
  48. y: 600,
  49. };
  50. const style = { width: 800, margin: 20 };
  51. const requestData = useCallback(() => {
  52. const tableData = bigJson.data;
  53. const allKeys = [];
  54. for (let i = 0, len = tableData.length; i < len; ++i) {
  55. const key = `${i}`;
  56. tableData[i][rowKey] = key;
  57. allKeys.push(key);
  58. }
  59. const queue = [...tableData];
  60. while (queue.length) {
  61. const curNode = queue.shift();
  62. const someSelected = curNode[authPoint].some(item => item[checkName]);
  63. const everySelected = curNode[authPoint].every(item => item[checkName]);
  64. curNode.checkAll = {
  65. checked: curNode[authPoint] && curNode[authPoint].length && everySelected,
  66. indeterminate: someSelected,
  67. };
  68. const parentKey = curNode[rowKey];
  69. if (curNode.children.length) {
  70. for (let i = 0, len = curNode.children.length; i < len; ++i) {
  71. const key = `${parentKey}.${childrenRecordName}.${i}`;
  72. curNode.children[i][rowKey] = key;
  73. allKeys.push(key);
  74. }
  75. queue.push(...curNode.children);
  76. }
  77. }
  78. return {
  79. data: tableData,
  80. allKeys,
  81. };
  82. }, []);
  83. const data = useMemo(() => {
  84. const res = requestData();
  85. return res.data;
  86. }, []);
  87. return (
  88. <Table
  89. style={style}
  90. columns={columns}
  91. childrenRecordName={childrenRecordName}
  92. defaultExpandAllRows
  93. rowKey={rowKey}
  94. // expandedRowRender={(record, index, expanded) => (
  95. // <article style={{ margin: 0 }}>
  96. // <p>
  97. // {index}: {expanded ? 'expanded' : 'unexpanded'}
  98. // </p>
  99. // <p>{record.description}</p>
  100. // </article>
  101. // )}
  102. onExpand={(expanded, expandedRow, domEvent) => {
  103. domEvent && domEvent.stopPropagation();
  104. console.log(expanded, expandedRow, domEvent);
  105. }}
  106. onRow={(record, index) => {
  107. return {
  108. onClick: () => {
  109. console.log(`Row ${index} clicked: `, record);
  110. },
  111. };
  112. }}
  113. // rowSelection={true}
  114. // hideExpandedColumn={false}
  115. // expandCellFixed={true}
  116. dataSource={data}
  117. scroll={scroll}
  118. pagination={false}
  119. virtualized
  120. size={'middle'}
  121. />
  122. );
  123. };
  124. export default Demo;