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

GridVirtualizerPadding.vue

95 lines2.5 KB

1<template>
2 <button @click="show = !show">Toggle</button>
3 <button @click="rowVirtualizer.scrollToIndex(halfWay)">
4 Scroll to index {{ halfWay }}
5 </button>
6 <button @click="rowVirtualizer.scrollToIndex(rows.length - 1)">
7 Scroll to index {{ rows.length - 1 }}
8 </button>
9 <br />
10 <br />
11​
12 <div
13 v-if="show"
14 ref="parentRef"
15 class="List"
16 style="height: 400px; width: 500px; overflow: auto"
17 >
18 <div
19 :style="{
20 height: `${totalSizeRows}px`,
21 width: `${totalSizeColumns}px`,
22 position: 'relative',
23 }"
24 >
25 <template v-for="virtualRow in virtualRows" :key="virtualRow.index">
26 <div
27 v-for="virtualColumn in columnVirtualizer.getVirtualItems()"
28 :key="virtualColumn.index"
29 :class="
30 virtualColumn.index % 2
31 ? virtualRow.index % 2 === 0
32 ? 'ListItemOdd'
33 : 'ListItemEven'
34 : virtualRow.index % 2
35 ? 'ListItemOdd'
36 : 'ListItemEven'
37 "
38 :style="{
39 position: 'absolute',
40 top: 0,
41 left: 0,
42 width: `${virtualColumn.size}px`,
43 height: `${virtualRow.size}px`,
44 transform: `translateX(${virtualColumn.start}px) translateY(${virtualRow.start}px)`,
45 }"
46 >
47 Cell {{ virtualRow.index }}, {{ virtualColumn.index }}
48 </div>
49 </template>
50 </div>
51 </div>
52</template>
53​
54<script setup lang="ts">
55import { ref, computed, PropType } from 'vue'
56import { useVirtualizer } from '@tanstack/vue-virtual'
57​
58const props = defineProps({
59 rows: {
60 type: Array as PropType<number[]>,
61 default: () => [],
62 },
63 columns: {
64 type: Array as PropType<number[]>,
65 default: () => [],
66 },
67})
68const show = ref(true)
69​
70const halfWay = computed(() => Math.floor(props.rows.length / 2))
71​
72const parentRef = ref<HTMLElement | null>(null)
73​
74const rowVirtualizer = useVirtualizer({
75 count: props.rows.length,
76 getScrollElement: () => parentRef.value,
77 estimateSize: () => 50,
78 paddingStart: 200,
79 paddingEnd: 200,
80})
81​
82const columnVirtualizer = useVirtualizer({
83 horizontal: true,
84 count: props.columns.length,
85 getScrollElement: () => parentRef.value,
86 estimateSize: () => 50,
87 paddingStart: 200,
88 paddingEnd: 200,
89})
90​
91const virtualRows = computed(() => rowVirtualizer.value.getVirtualItems())
92const totalSizeRows = computed(() => rowVirtualizer.value.getTotalSize())
93​
94const totalSizeColumns = computed(() => columnVirtualizer.value.getTotalSize())
95</script>