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

app.component.ts

194 lines5.1 KB

1import {
2 ChangeDetectionStrategy,
3 Component,
4 ElementRef,
5 computed,
6 signal,
7 viewChild,
8} from '@angular/core'
9import { injectVirtualizer } from '@tanstack/angular-virtual'
10import {
11 ColumnDef,
12 createAngularTable,
13 getCoreRowModel,
14 getSortedRowModel,
15 SortingState,
16 FlexRenderDirective,
17 SortDirection,
18} from '@tanstack/angular-table'
19import { makeData, type Person } from './make-data'
20​
21@Component({
22 selector: 'app-root',
23 standalone: true,
24 changeDetection: ChangeDetectionStrategy.OnPush,
25 imports: [FlexRenderDirective],
26 template: `
27 <p>
28 For tables, the basis for the offset of the translate css function is from
29 the row's initial position itself. Because of this, we need to calculate
30 the translateY pixel count different and base it off the the index.
31 </p>
32 <br />
33​
34 <div #scrollElement class="list scroll-container">
35 <div [style.height.px]="virtualizer.getTotalSize()">
36 <table>
37 <thead>
38 @for (
39 headerGroup of table.getHeaderGroups();
40 track headerGroup.id
41 ) {
42 <tr>
43 @for (header of headerGroup.headers; track header.id) {
44 <th
45 [attr.colspan]="header.colSpan"
46 [style.width.px]="header.getSize()"
47 >
48 @if (!header.isPlaceholder) {
49 <div
50 [class.cursor-pointer]="header.column.getCanSort()"
51 [class.select-none]="header.column.getCanSort()"
52 (click)="
53 header.column.getToggleSortingHandler()?.($event)
54 "
55 >
56 <ng-container
57 *flexRender="
58 header.column.columnDef.header;
59 props: header.getContext();
60 let headerText
61 "
62 >
63 <span>{{ headerText }}</span>
64 {{ getSortIcon(header.column.getIsSorted()) }}
65 </ng-container>
66 </div>
67 }
68 </th>
69 }
70 </tr>
71 }
72 </thead>
73 <tbody>
74 @for (
75 virtualRow of virtualizer.getVirtualItems();
76 track data[virtualRow.index].id
77 ) {
78 <tr
79 [style.height.px]="virtualRow.size"
80 [style.transform]="
81 'translateY(' +
82 (virtualRow.start - $index * virtualRow.size) +
83 'px)'
84 "
85 >
86 @for (
87 cell of rows()[virtualRow.index].getVisibleCells();
88 track cell.id
89 ) {
90 <td>
91 <ng-container
92 *flexRender="
93 cell.column.columnDef.cell;
94 props: cell.getContext();
95 let cellText
96 "
97 >
98 <span>{{ cellText }}</span>
99 </ng-container>
100 </td>
101 }
102 </tr>
103 }
104 </tbody>
105 </table>
106 </div>
107 </div>
108 `,
109 styles: `
110 .scroll-container {
111 height: 600px;
112 overflow: auto;
113 }
114 `,
115})
116export class AppComponent {
117 data = makeData(50_000)
118​
119 scrollElement = viewChild<ElementRef<HTMLDivElement>>('scrollElement')
120​
121 sorting = signal<SortingState>([])
122​
123 sortIcons = { asc: 'πŸ”Ό', desc: 'πŸ”½' }
124​
125 getSortIcon(sorting: false | SortDirection) {
126 return sorting ? this.sortIcons[sorting] : null
127 }
128​
129 columns: ColumnDef<Person>[] = [
130 {
131 accessorKey: 'id',
132 header: 'ID',
133 size: 60,
134 },
135 {
136 accessorKey: 'firstName',
137 cell: (info) => info.getValue(),
138 },
139 {
140 accessorFn: (row) => row.lastName,
141 id: 'lastName',
142 cell: (info) => info.getValue(),
143 header: 'Last Name',
144 },
145 {
146 accessorKey: 'age',
147 header: () => 'Age',
148 size: 50,
149 },
150 {
151 accessorKey: 'visits',
152 header: 'Visits',
153 size: 50,
154 },
155 {
156 accessorKey: 'status',
157 header: 'Status',
158 },
159 {
160 accessorKey: 'progress',
161 header: 'Profile Progress',
162 size: 80,
163 },
164 {
165 accessorKey: 'createdAt',
166 header: 'Created At',
167 cell: (info) => info.getValue<Date>().toLocaleString(),
168 },
169 ]
170​
171 table = createAngularTable(() => ({
172 data: this.data,
173 columns: this.columns,
174 state: {
175 sorting: this.sorting(),
176 },
177 onSortingChange: (updaterOrValue) =>
178 typeof updaterOrValue === 'function'
179 ? this.sorting.update(updaterOrValue)
180 : this.sorting.set(updaterOrValue),
181 getCoreRowModel: getCoreRowModel(),
182 getSortedRowModel: getSortedRowModel(),
183 debugTable: true,
184 }))
185​
186 rows = computed(() => this.table.getRowModel().rows)
187​
188 virtualizer = injectVirtualizer(() => ({
189 scrollElement: this.scrollElement(),
190 count: this.data.length,
191 estimateSize: () => 34,
192 overscan: 20,
193 }))