PrimeNG p-table Virtual Scroll and Angular OnPush Change Detection Interoperability
0 reputation · 24 Aug 2020, 10:12 UTC
0 reputation · 24 Aug 2020, 10:12 UTC
Render a PrimeNG p-table with virtualScroll enabled inside an Angular component using ChangeDetectionStrategy.OnPush. Data updates arrive asynchronously and replace the value input with a new array reference.
PrimeNG documentation does not explicitly confirm OnPush compatibility for virtual scroll. The table tracks rows through IterableDiffer, but virtualized DOM recycling may depend on change detection running or a scroll event forcing a visible-region recalculation. Behavior may vary between PrimeNG versions, and Angular v18+ zoneless change detection remains experimental.
35775 reputation · 24 Aug 2020, 13:06 UTC
[value] input with a new array reference does trigger Angular's OnPush change detection for the host component, which in turn causes the p-table to re-evaluate its virtual scroll and refresh the viewport. No manual ChangeDetectorRef call is required for this basic case.setTimeout with NgZone.runOutsideAngular, or Angular v18+ experimental zoneless change detection), the OnPush component will not be checked automatically. In those scenarios you must call markForCheck() or detectChanges() on the host component.detectChanges(). v18 further stabilizes the ngOnChanges handling, yet zoneless support remains experimental and may introduce timing differences.@Input reference changes. Passing a new array to [value] satisfies this condition.TableVirtualScroll recalculates visible rows during the change detection cycle (inside ngOnChanges or ngDoCheck). The component does not listen for array mutations; it relies solely on input reference changes.ngOnChanges now resets the virtual scroll calculator more reliably, reducing the need for manual intervention.p-table is nested inside another OnPush component, the parent must also receive a new reference or be marked for check.ChangeDetectionStrategy.OnPush and a p-table with virtualScroll.this.data = [...newData]).ChangeDetectorRef methods.provideExperimentalZonelessChangeDetection() to see if manual markForCheck() becomes necessary.Are you running with Angular's experimental zoneless change detection (provideExperimentalZonelessChangeDetection()) or deliberately updating data outside NgZone? If yes, manual markForCheck() is required. If no, the immutable reference update is sufficient.
// Inside an OnPush component
updateData(newItems: Item[]) {
this.data = [...newItems]; // new reference
// Only needed if update runs outside NgZone or in zoneless mode:
// this.cdr.markForCheck();
}Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.