VirtualScroll ExpressionChangedAfterItHasBeenCheckedError
I'm submitting a ... (check one with "x")
[x] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35
Plunkr Case (Bug Reports) https://stackblitz.com/edit/github-f1jpot
Current behavior Visually everything seems to work but there is an error in the console.
Expected behavior No errors in the console.
Minimal reproduction of the problem with instructions Just check the plunkr provided above
- Angular version: 9.1.9
- PrimeNG version: 9.1.0
- Browser: [all]
- Language: [all]
Same issue here, seems to be any 9.x version that is having this issue This causes an issue with the lazy load and scrolling - scrolling is completely broken.
- Angular version: 9.1.7
- PrimeNG version: 9.1.0
- Browser: [all]
- Language: [all]
this is my temporary fix, the key seems to be in this.vList.length = res.nb;
Still, the onLazyLoad event gets called twice the first time and i still get the error!
Array.prototype.splice.apply(this.vList, [...[event.first, event.rows], ...res.list]);
this.vList.length = res.nb;
this.vList= [...this.vList];
this.loading = false;
I get this error when the number of items changes. Or, for example, if you get the data from a backend and you don't know how many items there are, so you first make a request for the number of items:
ngOnInit() {
this.getNumberOfItems().subscribe(
totalCount => {
this.items = Array.from({ length: totalCount });
}
);
}
When the callback is called, the error is thrown.
This error still occurs in 10.0.0-rc2. Trying to implement virtual scroll with lazy, using data from a rest API request that supports paging, filtering, and sorting, is currently not possible due to the errors described above:
- The onLazyLoad event gets called twice the first time.
- The ExpressionChangedAfterItHasBeenCheckedError that is thrown every time the number of record changes (when you filter the records for example)
Note: The examples on the site that have Virtual-Scroll and Lazy all work because they don't try to sort/filter the results and because the examples don't try to simulate network calls.
Would be great to have an example like this one but with virtual-scroll and lazy to simulate a realistic experience where filtering/sorting/paging are done on the server.
there is some unchecked change-detection loop going on with the cdk virtual scroll... CdkVirtualScrollViewport... when changing the # of items in the [value] list, it recalculates the total px height of the inner list and that height change itself must be triggering something else... I wish I was smart enough to figure this out on my own.
Taking ownership.
The error is caused by the cdk scroller, there is a binding in the viewport component of cdk that updates the total height of the viewport. When the value changes, the binding causes this well known issue. So until this is fixed at cdk side, one workaround is calling the change detector manually.
export class TableVirtualScrollDemo {
public list: Entity[] = [];
constructor(private carService: CarService, private cd: ChangeDetectorRef) {}
public loadDataOnScroll($event: LazyLoadEvent) {
//timeout to simulate latency
setTimeout(() => {
this.list = [
{
text: "b"
}
];
this.cd.detectChanges();
}, 2000);
}
}
Reopening because I tried it with a regular table - cdk and it worked, something goes wrong when we use cdk scroll internally.