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

row-virtualizer-fixed.component.ts

52 lines1.4 KB

1import {
2 ChangeDetectionStrategy,
3 Component,
4 ElementRef,
5 viewChild,
6} from '@angular/core'
7import { injectVirtualizer } from '@tanstack/angular-virtual'
8​
9@Component({
10 standalone: true,
11 selector: 'row-virtualizer-fixed',
12 changeDetection: ChangeDetectionStrategy.OnPush,
13 template: `
14 <h3 style="margin-top: 2rem">Rows</h3>
15 <div #scrollElement class="list scroll-container">
16 <div
17 style="position: relative; width: 100%;"
18 [style.height.px]="virtualizer.getTotalSize()"
19 >
20 @for (row of virtualizer.getVirtualItems(); track row.index) {
21 <div
22 [attr.data-index]="row.index"
23 [class.list-item-even]="row.index % 2 === 0"
24 [class.list-item-odd]="row.index % 2 !== 0"
25 style="position: absolute; top: 0; left: 0; width: 100%;"
26 [style.height.px]="row.size"
27 [style.transform]="'translateY(' + row.start + 'px)'"
28 >
29 Row {{ row.index }}
30 </div>
31 }
32 </div>
33 </div>
34 `,
35 styles: `
36 .scroll-container {
37 height: 200px;
38 width: 400px;
39 overflow: auto;
40 }
41 `,
42})
43export class RowVirtualizerFixed {
44 scrollElement = viewChild<ElementRef<HTMLDivElement>>('scrollElement')
45​
46 virtualizer = injectVirtualizer(() => ({
47 scrollElement: this.scrollElement(),
48 count: 10000,
49 estimateSize: () => 35,
50 overscan: 5,
51 }))