index.jsx 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import { Table, Button } from '../../../index';
  3. function Demo() {
  4. const rowKey = record => `${record.city && record.city.toLowerCase()}-${record.job && record.job.toLowerCase()}`;
  5. const tableRef = useRef();
  6. const [data, setData] = useState([]);
  7. useEffect(() => {
  8. /**
  9. * FE => frontend engineer
  10. * BE => backend engineer
  11. * Andoird => android engineer
  12. * IOS => ios engineer
  13. * SE => software engineer
  14. */
  15. const data = [
  16. { city: 'Beijing', job: 'FE', department: 'IES' },
  17. { city: 'Beijing', job: 'BE', department: 'IES' },
  18. { city: 'Shanghai', job: 'Android', department: 'IES' },
  19. { city: 'Tokyo', job: 'Android', department: 'IES' },
  20. { city: 'Shanghai', job: 'IOS', department: 'EE' },
  21. { city: 'LA', job: 'SE', department: 'EE' },
  22. { city: 'Beijing', job: 'Android', department: 'EE' },
  23. { city: 'Tokyo', job: 'IOS', department: 'EE' },
  24. { city: 'Tokyo', job: 'SE', department: 'DATA' },
  25. { city: 'Shanghai', job: 'BE', department: 'DATA' },
  26. { city: 'LA', job: 'Android', department: 'DATA' },
  27. { city: 'LA', job: 'IOS', department: 'DATA' },
  28. ];
  29. setData(data);
  30. }, []);
  31. const columns = [
  32. { dataIndex: 'city', title: 'City', width: 400, sorter: (a, b) => (a.city > b.city ? 1 : -1) },
  33. {
  34. dataIndex: 'job',
  35. title: 'Job',
  36. width: 200,
  37. filters: [{ text: 'IOS', value: 'IOS' }, { text: 'Android', value: 'Android' }],
  38. onFilter: (value, record) => record.job && record.job.indexOf(value) === 0,
  39. },
  40. { dataIndex: 'department', title: 'Department' },
  41. ];
  42. return (
  43. <div style={{ padding: '20px 0px' }}>
  44. <div>
  45. <Button onClick={() => console.log(tableRef.current && tableRef.current.getCurrentPageData())}>
  46. 打印当前页数据
  47. </Button>
  48. </div>
  49. <Table
  50. dataSource={data}
  51. rowKey={rowKey}
  52. groupBy={'city'}
  53. columns={columns}
  54. renderGroupSection={groupKey => <strong>Jobs in {groupKey}:</strong>}
  55. onGroupedRow={(group, index) => {
  56. return {
  57. // onMouseEnter: () => {
  58. // console.log(`Grouped row mouse enter: `, group, index);
  59. // },
  60. // onMouseLeave: () => {
  61. // console.log(`Grouped row mouse leave: `, group, index);
  62. // },
  63. onClick: () => {
  64. console.log(`Grouped row mouse click: `, group, index);
  65. },
  66. };
  67. }}
  68. defaultExpandAllRows
  69. clickGroupedRowToExpand
  70. onExpand={(...args) => console.log('onExpand', args)}
  71. onExpandedRowsChange={(...args) => console.log('onExpandedRowsChange', args)}
  72. scroll={{ y: 480 }}
  73. ref={tableRef}
  74. />
  75. </div>
  76. );
  77. }
  78. export default Demo;