index.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { useState } from 'react';
  2. import { Table, Button, Space } from '../../../index';
  3. const initData = [
  4. { city: 'Beijing', job: 'FE', department: 'IES' },
  5. { city: 'Beijing', job: 'BE', department: 'IES' },
  6. { city: 'Shanghai', job: 'Android', department: 'IES' },
  7. { city: 'Tokyo', job: 'Android', department: 'IES' },
  8. { city: 'Shanghai', job: 'IOS', department: 'EE' },
  9. { city: 'LA', job: 'SE', department: 'EE' },
  10. { city: 'Beijing', job: 'Android', department: 'EE' },
  11. { city: 'Tokyo', job: 'IOS', department: 'EE' },
  12. { city: 'Tokyo', job: 'SE', department: 'DATA' },
  13. { city: 'Shanghai', job: 'BE', department: 'DATA' },
  14. { city: 'LA', job: 'Android', department: 'DATA' },
  15. { city: 'LA', job: 'IOS', department: 'DATA' },
  16. ];
  17. const columns = [
  18. { dataIndex: 'city', title: 'City', width: 400, sorter: (a, b) => (a.city > b.city ? 1 : -1) },
  19. {
  20. dataIndex: 'job',
  21. title: 'Job',
  22. width: 200,
  23. filters: [
  24. { text: 'IOS', value: 'IOS' },
  25. { text: 'Android', value: 'Android' },
  26. ],
  27. onFilter: (value, record) => record.job && record.job.indexOf(value) === 0,
  28. },
  29. { dataIndex: 'department', title: 'Department' },
  30. ];
  31. const rowKey = record => `${record.city && record.city.toLowerCase()}-${record.job && record.job.toLowerCase()}`;
  32. export default function App() {
  33. const [data, setData] = useState([]);
  34. const [expandAllGroupRows, setExpandAllGroupRows] = useState(true);
  35. const handleClick = () => {
  36. setData(Array.isArray(data) && data.length ? [] : initData);
  37. };
  38. const toggleExpandAllGroupRows = () => {
  39. setExpandAllGroupRows(!expandAllGroupRows);
  40. };
  41. return (
  42. <div style={{ padding: '20px 0px' }}>
  43. <Space>
  44. <Button onClick={handleClick}>
  45. {
  46. Array.isArray(data) && data.length ? '清空数据' : '加载数据'
  47. }
  48. </Button>
  49. <Button onClick={toggleExpandAllGroupRows}>动态设置expandAllGroupRows</Button>
  50. </Space><br /><br />
  51. <label>defaultExpandAllGroupRows</label>
  52. <Table
  53. dataSource={data}
  54. rowKey={rowKey}
  55. groupBy={'city'}
  56. columns={columns}
  57. renderGroupSection={groupKey => <strong>Jobs in {groupKey}:</strong>}
  58. onGroupedRow={(group, index) => ({
  59. onClick: e => {
  60. console.log('Grouped row clicked: ', group, index);
  61. }
  62. })}
  63. clickGroupedRowToExpand // if you want to click the entire row to expand
  64. scroll={{ y: 480 }}
  65. defaultExpandAllGroupRows
  66. />
  67. <label>expandAllGroupRows={`${expandAllGroupRows}`}</label>
  68. <Table
  69. dataSource={data}
  70. rowKey={rowKey}
  71. groupBy={'city'}
  72. columns={columns}
  73. renderGroupSection={groupKey => <strong>Jobs in {groupKey}:</strong>}
  74. onGroupedRow={(group, index) => ({
  75. onClick: e => {
  76. console.log('Grouped row clicked: ', group, index);
  77. }
  78. })}
  79. clickGroupedRowToExpand // if you want to click the entire row to expand
  80. scroll={{ y: 480 }}
  81. expandAllGroupRows={expandAllGroupRows}
  82. />
  83. </div>
  84. );
  85. }