-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathdemo-virtual.tsx
More file actions
123 lines (113 loc) · 3.03 KB
/
Copy pathdemo-virtual.tsx
File metadata and controls
123 lines (113 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React, { useState, useRef } from 'react'
import TableVirtual, {
TableColumnProps,
VirtualTableRef,
} from '../../index.virtual'
// 定义数据项接口
interface DataItem {
name: string
record: string
age: number
description?: string // 添加描述字段,用于测试不同高度的行
}
const DemoVirtual = () => {
// 创建表格引用
const tableRef = useRef<VirtualTableRef>(null)
// 生成大量数据
const generateData = (count: number): DataItem[] => {
const data: DataItem[] = []
for (let i = 0; i < count; i++) {
// 为部分行添加不同长度的描述,以测试动态高度
let description
if (i % 3 === 0) {
description = `这是一段较长的描述文本,用于测试动态高度。行号: ${i}. 这段文字会导致行高增加。`
} else if (i % 5 === 0) {
description = `这是一段非常非常长的描述文本,它会占用多行空间。这是为了测试虚拟滚动表格在处理不同高度的行时的表现。行号: ${i}. 我们希望表格能够正确计算每行的实际高度,并且在滚动时保持良好的性能和用户体验。`
} else {
description = undefined
}
data.push({
name: `Name ${i}`,
record: ['小学', '初中', '高中', '大专', '本科'][i % 5],
age: Math.floor(Math.random() * 50) + 10,
description,
})
}
return data
}
// 定义列配置
const [columns] = useState<TableColumnProps[]>([
{
title: 'ID',
key: 'id',
width: 50,
fixed: 'left',
render: (_record: any, index: number) => {
return index + 1
},
},
{
title: '姓名',
key: 'name',
width: 80,
},
{
title: '学历',
key: 'record',
width: 60,
},
{
title: '年龄',
key: 'age',
width: 70,
sorter: (a: DataItem, b: DataItem) => a.age - b.age,
},
{
title: '描述',
key: 'description',
width: 100,
fixed: 'right',
render: (record: DataItem) => {
return record.description ? (
<div
style={{
padding: '5px 0',
lineHeight: '1.5',
whiteSpace: 'normal',
wordBreak: 'break-word',
}}
>
{record.description}
</div>
) : (
'-'
)
},
},
])
// 使用状态管理数据
const [data, setData] = useState(generateData(1000))
// 更新数据的方法
const updateData = (count: number) => {
setData(generateData(count))
}
// 滚动到指定行的方法
const handleScrollToRow = (index: number) => {
if (tableRef.current) {
tableRef.current.scrollToIndex(index)
}
}
return (
<TableVirtual
columns={columns}
data={data}
virtual
height={400}
rowHeight={40} // 默认行高,实际会根据内容动态调整
overscan={10}
bordered
dynamicHeight // 启用动态高度
/>
)
}
export default DemoVirtual