forked from TanStack/virtual
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRowVirtualizerDynamic.vue
More file actions
82 lines (73 loc) · 2.21 KB
/
RowVirtualizerDynamic.vue
File metadata and controls
82 lines (73 loc) · 2.21 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
<template>
<div>
<button @click="rowVirtualizer.scrollToIndex(0)">scroll to the top</button>
<span style="padding: 0 4px"></span>
<button @click="rowVirtualizer.scrollToIndex(sentences.length / 2)">
scroll to the middle
</button>
<span style="padding: 0 4px"></span>
<button @click="rowVirtualizer.scrollToIndex(sentences.length - 1)">
scroll to the end
</button>
<hr />
<div
ref="parentRef"
class="List"
style="height: 400px; width: 400px; overflow-y: auto; contain: strict"
>
<div
:style="{
height: `${totalSize}px`,
width: '100%',
position: 'relative',
}"
>
<div
:style="{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
transform: `translateY(${virtualRows[0]?.start ?? 0}px)`,
}"
>
<div
v-for="virtualRow in virtualRows"
:key="virtualRow.key"
:data-index="virtualRow.index"
ref="virtualItemEls"
:class="virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'"
>
<div style="padding: 10px 0">
<div>Row {{ virtualRow.index }}</div>
<div>{{ sentences[virtualRow.index] }}</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, onUpdated, ref, shallowRef } from 'vue'
import { useVirtualizer } from '@tanstack/vue-virtual'
import { generateSentences } from './utils'
const sentences = generateSentences()
const parentRef = ref<HTMLElement | null>(null)
const rowVirtualizer = useVirtualizer({
count: sentences.length,
getScrollElement: () => parentRef.value,
estimateSize: () => 55,
})
const virtualRows = computed(() => rowVirtualizer.value.getVirtualItems())
const totalSize = computed(() => rowVirtualizer.value.getTotalSize())
const virtualItemEls = shallowRef([])
function measureAll() {
rowVirtualizer.value.measureElement(null)
virtualItemEls.value.forEach((el) => {
if (el) rowVirtualizer.value.measureElement(el)
})
}
onMounted(measureAll)
onUpdated(measureAll)
</script>