2019-11-15 14:35:25 +08:00
|
|
|
---
|
|
|
|
order: 30
|
|
|
|
title:
|
|
|
|
en-US: Virtual list
|
|
|
|
zh-CN: 虚拟列表
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
通过 `react-window` 引入虚拟滚动方案,实现 100000 条数据的高性能表格。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Integrate virtual scroll with `react-window` to achieve a high performance table of 100,000 data.
|
|
|
|
|
2019-12-17 17:54:10 +08:00
|
|
|
```tsx
|
2019-11-15 14:35:25 +08:00
|
|
|
import { Table } from 'antd';
|
2022-05-23 14:37:16 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import ResizeObserver from 'rc-resize-observer';
|
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
|
|
|
import { VariableSizeGrid as Grid } from 'react-window';
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const VirtualTable = (props: Parameters<typeof Table>[0]) => {
|
2020-07-20 15:35:15 +08:00
|
|
|
const { columns, scroll } = props;
|
2020-01-22 12:11:49 +08:00
|
|
|
const [tableWidth, setTableWidth] = useState(0);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2020-12-17 15:09:18 +08:00
|
|
|
const widthColumnCount = columns!.filter(({ width }) => !width).length;
|
|
|
|
const mergedColumns = columns!.map(column => {
|
2019-11-15 14:35:25 +08:00
|
|
|
if (column.width) {
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...column,
|
|
|
|
width: Math.floor(tableWidth / widthColumnCount),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-01-22 12:11:49 +08:00
|
|
|
const gridRef = useRef<any>();
|
|
|
|
const [connectObject] = useState<any>(() => {
|
2019-11-15 14:35:25 +08:00
|
|
|
const obj = {};
|
|
|
|
Object.defineProperty(obj, 'scrollLeft', {
|
2022-06-21 14:09:31 +08:00
|
|
|
get: () => {
|
|
|
|
if (gridRef.current) {
|
|
|
|
return gridRef.current?.state?.scrollLeft;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
2019-11-15 14:35:25 +08:00
|
|
|
set: (scrollLeft: number) => {
|
|
|
|
if (gridRef.current) {
|
|
|
|
gridRef.current.scrollTo({ scrollLeft });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
});
|
|
|
|
|
|
|
|
const resetVirtualGrid = () => {
|
2022-09-09 15:51:35 +08:00
|
|
|
gridRef.current?.resetAfterIndices({
|
2019-11-15 14:35:25 +08:00
|
|
|
columnIndex: 0,
|
2021-04-23 16:58:36 +08:00
|
|
|
shouldForceUpdate: true,
|
2019-11-15 14:35:25 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-01-22 12:11:49 +08:00
|
|
|
useEffect(() => resetVirtualGrid, [tableWidth]);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
const renderVirtualList = (rawData: object[], { scrollbarSize, ref, onScroll }: any) => {
|
|
|
|
ref.current = connectObject;
|
2020-09-17 16:05:08 +08:00
|
|
|
const totalHeight = rawData.length * 54;
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Grid
|
|
|
|
ref={gridRef}
|
|
|
|
className="virtual-grid"
|
|
|
|
columnCount={mergedColumns.length}
|
2020-12-17 15:09:18 +08:00
|
|
|
columnWidth={(index: number) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
const { width } = mergedColumns[index];
|
2020-12-17 15:09:18 +08:00
|
|
|
return totalHeight > scroll!.y! && index === mergedColumns.length - 1
|
|
|
|
? (width as number) - scrollbarSize - 1
|
|
|
|
: (width as number);
|
2019-11-15 14:35:25 +08:00
|
|
|
}}
|
2020-12-17 15:09:18 +08:00
|
|
|
height={scroll!.y as number}
|
2019-11-15 14:35:25 +08:00
|
|
|
rowCount={rawData.length}
|
2020-07-22 10:43:36 +08:00
|
|
|
rowHeight={() => 54}
|
2019-11-15 14:35:25 +08:00
|
|
|
width={tableWidth}
|
2020-12-17 15:09:18 +08:00
|
|
|
onScroll={({ scrollLeft }: { scrollLeft: number }) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
onScroll({ scrollLeft });
|
|
|
|
}}
|
|
|
|
>
|
2020-12-17 15:09:18 +08:00
|
|
|
{({
|
|
|
|
columnIndex,
|
|
|
|
rowIndex,
|
|
|
|
style,
|
|
|
|
}: {
|
|
|
|
columnIndex: number;
|
|
|
|
rowIndex: number;
|
|
|
|
style: React.CSSProperties;
|
|
|
|
}) => (
|
2019-11-15 14:35:25 +08:00
|
|
|
<div
|
|
|
|
className={classNames('virtual-table-cell', {
|
|
|
|
'virtual-table-cell-last': columnIndex === mergedColumns.length - 1,
|
|
|
|
})}
|
|
|
|
style={style}
|
|
|
|
>
|
2020-12-17 15:09:18 +08:00
|
|
|
{(rawData[rowIndex] as any)[(mergedColumns as any)[columnIndex].dataIndex]}
|
2019-11-15 14:35:25 +08:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Grid>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2019-12-17 17:54:10 +08:00
|
|
|
<ResizeObserver
|
|
|
|
onResize={({ width }) => {
|
|
|
|
setTableWidth(width);
|
|
|
|
}}
|
|
|
|
>
|
2019-11-15 14:35:25 +08:00
|
|
|
<Table
|
|
|
|
{...props}
|
2020-07-20 15:35:15 +08:00
|
|
|
className="virtual-table"
|
2019-11-15 14:35:25 +08:00
|
|
|
columns={mergedColumns}
|
|
|
|
pagination={false}
|
|
|
|
components={{
|
|
|
|
body: renderVirtualList,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</ResizeObserver>
|
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
// Usage
|
|
|
|
const columns = [
|
|
|
|
{ title: 'A', dataIndex: 'key', width: 150 },
|
2019-12-17 17:54:10 +08:00
|
|
|
{ title: 'B', dataIndex: 'key' },
|
|
|
|
{ title: 'C', dataIndex: 'key' },
|
|
|
|
{ title: 'D', dataIndex: 'key' },
|
2019-11-15 14:35:25 +08:00
|
|
|
{ title: 'E', dataIndex: 'key', width: 200 },
|
|
|
|
{ title: 'F', dataIndex: 'key', width: 100 },
|
|
|
|
];
|
|
|
|
|
2020-06-18 13:42:21 +08:00
|
|
|
const data = Array.from({ length: 100000 }, (_, key) => ({ key }));
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => (
|
2022-04-03 23:27:45 +08:00
|
|
|
<VirtualTable columns={columns} dataSource={data} scroll={{ y: 300, x: '100vw' }} />
|
2019-11-15 14:35:25 +08:00
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
export default App;
|
2019-11-15 14:35:25 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.virtual-table .ant-table-container:before,
|
|
|
|
.virtual-table .ant-table-container:after {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
.virtual-table-cell {
|
|
|
|
box-sizing: border-box;
|
|
|
|
padding: 16px;
|
|
|
|
border-bottom: 1px solid #e8e8e8;
|
|
|
|
background: #FFF;
|
|
|
|
}
|
2019-12-25 15:14:34 +08:00
|
|
|
[data-theme="dark"] .virtual-table-cell {
|
|
|
|
box-sizing: border-box;
|
|
|
|
padding: 16px;
|
|
|
|
border-bottom: 1px solid #303030;
|
|
|
|
background: #141414;
|
|
|
|
}
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
</style>
|