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

row-virtualizer-variable.component.ts

55 lines1.5 KB

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