Skip to content
Source
Sign inSign up
TanStack/virtual
Open History to see the last commit

RowVirtualizerVariable.vue

51 lines1.3 KB

1<template>
2 <div
3 ref="parentRef"
4 class="List"
5 style="height: 200px; width: 400px; overflow: auto"
6 >
7 <div
8 :style="{ height: `${totalSize}px`, width: '100%', position: 'relative' }"
9 >
10 <div
11 v-for="virtualRow in virtualRows"
12 :key="virtualRow.index"
13 :class="virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'"
14 :style="{
15 position: 'absolute',
16 top: 0,
17 left: 0,
18 width: '100%',
19 height: `${virtualRow.size}px`,
20 transform: `translateY(${virtualRow.start}px)`,
21 }"
22 >
23 Row {{ virtualRow.index }}
24 </div>
25 </div>
26 </div>
27</template>
28​
29<script setup lang="ts">
30import { ref, computed, PropType } from 'vue'
31import { useVirtualizer } from '@tanstack/vue-virtual'
32​
33const props = defineProps({
34 rows: {
35 type: Array as PropType<number[]>,
36 default: () => [],
37 },
38})
39​
40const parentRef = ref<HTMLElement | null>(null)
41​
42const rowVirtualizer = useVirtualizer({
43 count: props.rows.length,
44 getScrollElement: () => parentRef.value,
45 estimateSize: (i) => props.rows[i],
46 overscan: 5,
47})
48​
49const virtualRows = computed(() => rowVirtualizer.value.getVirtualItems())
50const totalSize = computed(() => rowVirtualizer.value.getTotalSize())
51</script>