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

App.vue

180 lines4.5 KB

1<template>
2 <div>
3 <p>
4 For tables, the basis for the offset of the translate css function is from
5 the row's initial position itself. Because of this, we need to calculate
6 the translateY pixel count differently and base it off the index.
7 </p>
8​
9 <div ref="parentRef" class="container">
10 <div :style="{ height: `${totalSize}px` }">
11 <table>
12 <thead>
13 <tr
14 v-for="headerGroup in table.getHeaderGroups()"
15 :key="headerGroup.id"
16 >
17 <th
18 v-for="header in headerGroup.headers"
19 :key="header.id"
20 :colspan="header.colSpan"
21 :style="{ width: `${header.getSize()}px` }"
22 >
23 <div
24 v-if="!header.isPlaceholder"
25 :class="[
26 'text-left',
27 header.column.getCanSort()
28 ? 'cursor-pointer select-none'
29 : '',
30 ]"
31 @click="
32 getSortingHandler(
33 $event,
34 header.column.getToggleSortingHandler(),
35 )
36 "
37 >
38 <FlexRender
39 :render="header.column.columnDef.header"
40 :props="header.getContext()"
41 />
42 <span v-if="header.column.getIsSorted() === 'asc'"> πŸ”Ό</span>
43 <span v-if="header.column.getIsSorted() === 'desc'"> πŸ”½</span>
44 </div>
45 </th>
46 </tr>
47 </thead>
48 <tbody>
49 <tr
50 v-for="(virtualRow, index) in virtualRows"
51 :key="virtualRow.key"
52 :style="{
53 transform: `translateY(${virtualRow.start - index * virtualRow.size}px)`,
54 }"
55 >
56 <td
57 v-for="cell in rows[virtualRow.index].getVisibleCells()"
58 :key="cell.id"
59 >
60 <FlexRender
61 :render="cell.column.columnDef.cell"
62 :props="cell.getContext()"
63 />
64 </td>
65 </tr>
66 </tbody>
67 </table>
68 </div>
69 </div>
70 </div>
71</template>
72​
73<script setup lang="ts">
74import { ref, computed } from 'vue'
75import { useVirtualizer } from '@tanstack/vue-virtual'
76import {
77 FlexRender,
78 ColumnDef,
79 SortingState,
80 useVueTable,
81 getCoreRowModel,
82 getSortedRowModel,
83} from '@tanstack/vue-table'
84import { makeData, Person } from './makeData'
85​
86const data = ref(makeData(50_000))
87​
88const sorting = ref<SortingState>([])
89​
90const getSortingHandler = (e: Event, fn: any) => {
91 return fn(e)
93​
94const setSorting = (sortingUpdater: any) => {
95 const newSortVal = sortingUpdater(sorting.value)
96​
97 sorting.value = newSortVal
99​
100const columns = computed<ColumnDef<Person>[]>(() => {
101 return [
102 {
103 accessorKey: 'id',
104 header: 'ID',
105 size: 60,
106 },
107 {
108 accessorKey: 'firstName',
109 cell: (info) => info.getValue(),
110 },
111 {
112 accessorFn: (row) => row.lastName,
113 id: 'lastName',
114 cell: (info) => info.getValue(),
115 header: () => 'Last Name',
116 },
117 {
118 accessorKey: 'age',
119 header: () => 'Age',
120 size: 50,
121 },
122 {
123 accessorKey: 'visits',
124 header: () => 'Visits',
125 size: 50,
126 },
127 {
128 accessorKey: 'status',
129 header: 'Status',
130 },
131 {
132 accessorKey: 'progress',
133 header: 'Profile Progress',
134 size: 80,
135 },
136 {
137 accessorKey: 'createdAt',
138 header: 'Created At',
139 cell: (info) => info.getValue<Date>().toLocaleString(),
140 },
141 ]
142})
143​
144const table = useVueTable({
145 get data() {
146 return data.value
147 },
148 columns: columns.value,
149 state: {
150 get sorting() {
151 return sorting.value
152 },
153 },
154 onSortingChange: setSorting,
155 getCoreRowModel: getCoreRowModel(),
156 getSortedRowModel: getSortedRowModel(),
157 debugTable: true,
158})
159​
160const rows = computed(() => {
161 return table.getRowModel().rows
162})
163​
164const parentRef = ref<HTMLElement | null>(null)
165​
166const rowVirtualizerOptions = computed(() => {
167 return {
168 count: rows.value.length,
169 getScrollElement: () => parentRef.value,
170 estimateSize: () => 34,
171 overscan: 5,
172 }
173})
174​
175const rowVirtualizer = useVirtualizer(rowVirtualizerOptions)
176​
177const virtualRows = computed(() => rowVirtualizer.value.getVirtualItems())
178​
179const totalSize = computed(() => rowVirtualizer.value.getTotalSize())
180</script>