virtualized.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React, { useRef } from 'react';
  2. import { Table, Avatar, Button } from '@douyinfe/semi-ui';
  3. const DAY = 24 * 60 * 60 * 1000;
  4. const columns = [
  5. {
  6. title: '标题',
  7. dataIndex: 'name',
  8. width: 200,
  9. fixed: true,
  10. render: (text, record, index) => {
  11. return (
  12. <div>
  13. {text}
  14. </div>
  15. );
  16. },
  17. filters: [
  18. {
  19. text: 'Semi Design 设计稿',
  20. value: 'Semi Design 设计稿',
  21. },
  22. {
  23. text: 'Semi Pro 设计稿',
  24. value: 'Semi Pro 设计稿',
  25. },
  26. ],
  27. onFilter: (value, record) => record.name.includes(value),
  28. },
  29. {
  30. title: '大小',
  31. dataIndex: 'size',
  32. width: 150,
  33. sorter: (a, b) => a.size - b.size > 0 ? 1 : -1,
  34. render: (text) => `${text} KB`
  35. },
  36. {
  37. title: '所有者',
  38. dataIndex: 'owner',
  39. render: (text, record, index) => {
  40. return (
  41. <div>
  42. <Avatar size="small" color={record.avatarBg} style={{ marginRight: 4 }}>{typeof text === 'string' && text.slice(0, 1)}</Avatar>
  43. {text}
  44. </div>
  45. );
  46. }
  47. },
  48. {
  49. title: '更新日期',
  50. dataIndex: 'updateTime',
  51. fixed: 'right',
  52. width: 150,
  53. sorter: (a, b) => a.updateTime - b.updateTime > 0 ? 1 : -1,
  54. render: (value) => {
  55. return dateFns.format(new Date(value), 'yyyy-MM-dd');
  56. }
  57. }
  58. ];
  59. const getData = () => {
  60. const data = [];
  61. for (let i = 0; i < 1000; i++) {
  62. const isSemiDesign = i % 2 === 0;
  63. const randomNumber = (i * 1000) % 199;
  64. data.push({
  65. key: '' + i,
  66. name: isSemiDesign ? `Semi Design 设计稿${i}.fig` : `Semi Pro 设计稿${i}.fig`,
  67. owner: isSemiDesign ? '姜鹏志' : '郝宣',
  68. size: randomNumber,
  69. updateTime: new Date().valueOf() + randomNumber * DAY,
  70. avatarBg: isSemiDesign ? 'grey' : 'red'
  71. });
  72. }
  73. return data;
  74. };
  75. const data = getData();
  76. function VirtualizedFixedDemo() {
  77. let virtualizedListRef = useRef();
  78. const scroll = { y: 400, x: 900 };
  79. const style = { width: 750, margin: '0 auto' };
  80. return (
  81. <>
  82. <Button onClick={() => virtualizedListRef.current.scrollToItem(100)}>Scroll to 100</Button>
  83. <Table
  84. pagination={false}
  85. columns={columns}
  86. dataSource={data}
  87. scroll={scroll}
  88. style={style}
  89. virtualized
  90. getVirtualizedListRef={ref => virtualizedListRef = ref}
  91. />
  92. </>
  93. );
  94. }
  95. render(VirtualizedFixedDemo);