index.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { Table, Button } from '@douyinfe/semi-ui';
  2. import React, { useState, useCallback, useMemo, useEffect, useRef } from 'react';
  3. function App() {
  4. const [total, setTotal] = useState(406);
  5. const [pageSize, setPageSize] = useState(10);
  6. const [dataSource, setDataSource] = useState([]);
  7. const [currentPage, setCurrentPage] = useState(1);
  8. const [paginationHidden, setPaginationHidden] = useState(false);
  9. const tableRef = useRef();
  10. const pagination = useMemo(() => {
  11. if (paginationHidden) {
  12. return false;
  13. }
  14. return {
  15. showSizeChanger: true,
  16. pageSize,
  17. // currentPage,
  18. total,
  19. // onPageChange: currentPage => setCurrentPage(currentPage),
  20. formatPageText: ({ currentStart, currentEnd, total }) => `${currentStart} - ${currentEnd}, 共 ${total} 条`,
  21. onPageSizeChange: pageSize => setPageSize(pageSize),
  22. };
  23. }, [pageSize, paginationHidden]);
  24. const randomTotal = useCallback(() => {
  25. setTotal(Math.ceil(Math.random() * 100) + 40);
  26. });
  27. const columns = useMemo(() => [
  28. {
  29. title: 'Name',
  30. dataIndex: 'name',
  31. filters: [
  32. {
  33. text: '名字包含"1"',
  34. value: '1',
  35. },
  36. {
  37. text: '名字包含"2"',
  38. value: '2',
  39. },
  40. ],
  41. onFilter: (value, record) => record.name.indexOf(value) > -1,
  42. sorter: (a, b) => a.name.length - b.name.length,
  43. },
  44. {
  45. title: 'Age',
  46. dataIndex: 'age',
  47. sorter: (a, b) => (a.age - b.age > 0 ? 1 : -1),
  48. },
  49. {
  50. title: 'Address',
  51. dataIndex: 'address',
  52. filters: [
  53. {
  54. text: 'London',
  55. value: 'London',
  56. },
  57. {
  58. text: 'New York',
  59. value: 'New York',
  60. },
  61. ],
  62. filterMultiple: false,
  63. onFilter: (value, record) => record.address.indexOf(value) === 0,
  64. sorter: (a, b) => a.address.length - b.address.length,
  65. },
  66. ]);
  67. useEffect(() => {
  68. const data = [];
  69. const currentPage = 1;
  70. for (let i = 0; i < total; i++) {
  71. data.push({
  72. key: '' + i,
  73. name: `Edward King ${i}`,
  74. age: 32,
  75. address: `London, Park Lane no. ${i}`,
  76. });
  77. }
  78. setDataSource(data);
  79. setCurrentPage(1);
  80. }, [total]);
  81. useEffect(() => {
  82. if (dataSource.length) {
  83. }
  84. }, [currentPage]);
  85. const togglePagination = useCallback(() => {
  86. setPaginationHidden(!paginationHidden);
  87. }, [paginationHidden]);
  88. const randomPageSize = () => setPageSize(pageSize + Math.floor(10 * Math.random()));
  89. return (
  90. <div>
  91. <Button onClick={randomTotal}>随机数据总数</Button>
  92. <Button onClick={togglePagination}>{pagination ? '关闭' : '显示'}分页区域</Button>
  93. <Button onClick={randomPageSize}>随机页容量</Button>
  94. <Button
  95. onClick={() =>
  96. tableRef.current && console.log('CurrentPageData: ', tableRef.current.getCurrentPageData())
  97. }
  98. >
  99. 打印当前页数据
  100. </Button>
  101. <Table
  102. columns={columns}
  103. dataSource={dataSource}
  104. pagination={pagination}
  105. onChange={(...args) => console.log(...args)}
  106. ref={tableRef}
  107. scroll={{ y: 400, scrollToFirstRowOnChange: true }}
  108. />
  109. </div>
  110. );
  111. }
  112. export default App;