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

App.vue

121 lines3.1 KB

1<script setup lang="ts">
2import { computed, ref, watchEffect } from 'vue'
3import { useVirtualizer } from '@tanstack/vue-virtual'
4import { useInfiniteQuery } from '@tanstack/vue-query'
5​
6const fetchServerPage = async (
7 limit: number,
8 offset: number = 0,
9): Promise<{ rows: string[]; nextOffset: number }> => {
10 const rows = new Array(limit)
11 .fill(0)
12 .map((e, i) => `Async loaded row #${i + offset * limit}`)
13​
14 await new Promise((r) => setTimeout(r, 500))
15​
16 return { rows, nextOffset: offset + 1 }
18​
19const {
20 status,
21 data,
22 error,
23 isFetching,
24 isFetchingNextPage,
25 fetchNextPage,
26 hasNextPage,
27} = useInfiniteQuery({
28 queryKey: ['projects'],
29 queryFn: (ctx) => fetchServerPage(10, ctx.pageParam),
30 getNextPageParam: (_lastGroup, groups) => groups.length,
31})
32​
33const allRows = computed(() =>
34 data.value ? data.value.pages.flatMap((d) => d.rows) : [],
36​
37const parentRef = ref<HTMLElement | null>(null)
38​
39const rowVirtualizerOptions = computed(() => {
40 return {
41 count: hasNextPage ? allRows.value.length + 1 : allRows.value.length,
42 getScrollElement: () => parentRef.value,
43 estimateSize: () => 100,
44 overscan: 5,
45 }
46})
47​
48const rowVirtualizer = useVirtualizer(rowVirtualizerOptions)
49​
50const virtualRows = computed(() => rowVirtualizer.value.getVirtualItems())
51​
52const totalSize = computed(() => rowVirtualizer.value.getTotalSize())
53​
54watchEffect(() => {
55 const [lastItem] = [...virtualRows.value].reverse()
56​
57 if (!lastItem) {
58 return
59 }
60​
61 if (
62 lastItem.index >= allRows.value.length - 1 &&
63 hasNextPage.value &&
64 !isFetchingNextPage.value
65 ) {
66 fetchNextPage()
67 }
68})
69</script>
70​
71<template>
72 <div>
73 <p>
74 This infinite scroll example uses Vue Query's useInfiniteScroll composable
75 to fetch infinite data from a posts endpoint and then a rowVirtualizer is
76 used along with a loader-row placed at the bottom of the list to trigger
77 the next page to load.
78 </p>
79​
80 <br />
81 <br />
82 <p v-if="status === 'loading'">Loading...</p>
83 <p v-else-if="status === 'error'">Error: {{ (error as Error).message }}</p>
84 <div
85 v-else
86 ref="parentRef"
87 class="List"
88 style="height: 500px; width: 100%; overflow: auto"
89 >
90 <div
91 :style="{
92 height: `${totalSize}px`,
93 width: '100%',
94 position: 'relative',
95 }"
96 >
97 <div
98 v-for="virtualRow in virtualRows"
99 :key="virtualRow.key"
100 :class="virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'"
101 :style="{
102 position: 'absolute',
103 top: 0,
104 left: 0,
105 width: '100%',
106 height: `${virtualRow.size}px`,
107 transform: `translateY(${virtualRow.start}px)`,
108 }"
109 >
110 <template v-if="virtualRow.index > allRows.length - 1">
111 {{ hasNextPage ? 'Loading more...' : 'Nothing more to load' }}
112 </template>
113 <template v-else>
114 {{ allRows[virtualRow.index] }}
115 </template>
116 </div>
117 </div>
118 </div>
119 <div v-if="isFetching && !isFetchingNextPage">Background Updating...</div>
120 </div>
121</template>