The way I did it is using the (undocumented) "last" local variable that is set to true for the last row in the table. Using it I add a "last-row" class to an element in the last row. Then using MutationObserver I detect when that element is inserted in DOM.
Even if this is basically a pooling because the MutationObserver handler is executed every time when there is a DOM change, we are able to execute a piece of code when the last row is inserted in DOM. I haven't tried it with a Paginator yet.
I got the idea to use "last" local variable seeing that ngFor has it and I thought that table should be implemented as an ngFor or it should use one ...
<table mat-table [dataSource]="(results$ | async)?.drivers" class="mat-elevation-z8">
<ng-container matColumnDef="colId">
<th mat-header-cell *matHeaderCellDef> ID </th>
<td class="cell-id" mat-cell *matCellDef="let driver; let i = index; let isLastRow = last">
<div class="no-wrap-ellipsis" [ngClass]="{'last-row': isLastRow}">{{driver.driverId}}</div>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="scheduleTable_displayedColumns; sticky: true;"></tr>
<tr mat-row *matRowDef="let row; columns: scheduleTable_displayedColumns;"></tr>
protected mutationObserverToDetectLastRow: MutationObserver;
ngOnInit(): void {
this.initMutationObserverToDetectLastRow();
}
private initMutationObserverToDetectLastRow = () => {
this.mutationObserverToDetectLastRow = new MutationObserver(_ => this.mutationObserverToDetectLastRowHandler());
this.mutationObserverToDetectLastRow.observe(document, { attributes: false, childList: true, characterData: false, subtree: true });
}
protected mutationObserverToDetectLastRowHandler = () => {
if (document.body.contains(document.body.querySelector(".last-row"))) {
console.log("Last row was displayed");
}
this.mutationObserverToDetectLastRow.disconnect();
}
ngOnDestroy(): void {
this.mutationObserverToDetectLastRow.disconnect();
}