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

grid-virtualizer-padding.component.ts

122 lines3.3 KB

1import {
2 ChangeDetectionStrategy,
3 Component,
4 ElementRef,
5 effect,
6 input,
7 signal,
8 viewChild,
9 viewChildren,
10} from '@angular/core'
11import { injectVirtualizer } from '@tanstack/angular-virtual'
12​
13@Component({
14 standalone: true,
15 selector: 'grid-virtualizer-padding',
16 changeDetection: ChangeDetectionStrategy.OnPush,
17 template: `
18 <h3 style="margin-top: 2rem">Grid</h3>
19 <button type="button" (click)="toggleShow()">Toggle</button>
20 <button
21 type="button"
22 (click)="rowVirtualizer.scrollToIndex(rows().length / 2)"
23 >
24 Scroll to the middle
25 </button>
26 <button
27 type="button"
28 (click)="rowVirtualizer.scrollToIndex(rows().length - 1)"
29 >
30 Scroll to the end
31 </button>
32 @if (show()) {
33 <div #scrollElement class="list scroll-container">
34 <div
35 style="position: relative;"
36 [style.height.px]="rowVirtualizer.getTotalSize()"
37 [style.width.px]="columnVirtualizer.getTotalSize()"
38 >
39 @for (row of rowVirtualizer.getVirtualItems(); track row.index) {
40 @for (col of columnVirtualizer.getVirtualItems(); track col.index) {
41 <div
42 #virtualItem
43 [attr.data-colindex]="col.index"
44 [attr.data-rowindex]="row.index"
45 [class]="
46 col.index % 2
47 ? row.index % 2 === 0
48 ? 'list-item-odd'
49 : 'list-item-even'
50 : row.index % 2
51 ? 'list-item-odd'
52 : 'list-item-even'
53 "
54 style="position: absolute; top: 0; left: 0;"
55 [style.width.px]="columns()[col.index]"
56 [style.height.px]="rows()[row.index]"
57 [style.transform]="
58 'translateX(' +
59 col.start +
60 'px) translateY(' +
61 row.start +
62 'px)'
63 "
64 >
65 <div>Cell {{ row.index }}, {{ col.index }}</div>
66 </div>
67 }
68 }
69 </div>
70 </div>
71 }
72 `,
73 styles: `
74 .scroll-container {
75 height: 400px;
76 width: 500px;
77 overflow: auto;
78 }
79 `,
80})
81export class GridVirtualizerPadding {
82 rows = input.required<number[]>()
83 columns = input.required<number[]>()
84​
85 scrollElement = viewChild<ElementRef<HTMLDivElement>>('scrollElement')
86​
87 rowVirtualizer = injectVirtualizer(() => ({
88 scrollElement: this.scrollElement(),
89 count: this.rows().length,
90 estimateSize: (index) => this.rows()[index]!,
91 overscan: 5,
92 paddingStart: 200,
93 paddingEnd: 200,
94 indexAttribute: 'data-rowindex',
95 }))
96​
97 columnVirtualizer = injectVirtualizer(() => ({
98 horizontal: true,
99 scrollElement: this.scrollElement(),
100 count: this.columns().length,
101 estimateSize: (index) => this.columns()[index]!,
102 overscan: 5,
103 paddingStart: 200,
104 paddingEnd: 200,
105 indexAttribute: 'data-colindex',
106 }))
107​
108 virtualItems = viewChildren<ElementRef<HTMLDivElement>>('virtualItem')
109​
110 #measureItems = effect(() =>
111 this.virtualItems().forEach((el) => {
112 this.rowVirtualizer.measureElement(el.nativeElement)
113 this.columnVirtualizer.measureElement(el.nativeElement)
114 }),
115 )
116​
117 show = signal(true)
118​
119 toggleShow() {
120 this.show.update((show) => !show)
121 }