Skip to content

Commit 0e9ffb4

Browse files
JohnTrunixspike-rabbit
authored andcommitted
fix(body): prevent ghost rows from overflowing the body
Only fill the fully visible rows with ghost rows and clamp the last rendered row index to the row count, so ghost loading no longer causes a vertical scrollbar. Apply the body height to the ghost loader overlay, so that it clips the rows that do not fit. (cherry picked from commit 4177f57)
1 parent 0598958 commit 0e9ffb4

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

‎projects/ngx-datatable/src/lib/components/body/body.component.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,10 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
669669
// that shows up inside the view port the last.
670670
const height = parseInt(this._bodyHeight(), 10);
671671
first = this.rowHeightsCache().getRowIndex(this.offsetY());
672-
last = this.rowHeightsCache().getRowIndex(height + this.offsetY()) + 1;
672+
last = Math.min(
673+
this.rowHeightsCache().getRowIndex(height + this.offsetY()) + 1,
674+
this.rowCount()
675+
);
673676
}
674677
} else {
675678
// The server is handling paging and will pass an array that begins with the

‎projects/ngx-datatable/src/lib/components/body/ghost-loader/ghost-loader.component.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class DataTableGhostLoaderComponent {
1313
readonly columns = input.required<TableColumnInternal[]>();
1414
readonly pageSize = input.required<number, unknown>({ transform: numberAttribute });
1515
readonly rowHeight = input.required<number | 'auto' | ((row?: any) => number)>();
16-
readonly ghostBodyHeight = input<number, unknown>(undefined, { transform: numberAttribute });
16+
readonly ghostBodyHeight = input<string>();
1717

1818
protected readonly ghostRows = computed(() =>
1919
Array.from({ length: this.pageSize() }, (_, index) => index)

‎projects/ngx-datatable/src/lib/components/datatable.component.spec.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ describe('DatatableComponent With Ghost Loading', () => {
668668
imports: [DatatableComponent],
669669
template: `
670670
<ngx-datatable
671+
style="block-size: 100%"
671672
columnMode="force"
672673
[rows]="rows()"
673674
[columns]="columns()"
@@ -703,17 +704,25 @@ describe('DatatableComponent With Ghost Loading', () => {
703704
datatable = fixture.debugElement.query(By.directive(DatatableComponent)).componentInstance;
704705
});
705706

706-
it('should push enough undefined rows to fill pageSize when ghost loading with no data', async () => {
707+
/** Waits for the body to be measured and returns the number of rows fitting into it completely. */
708+
const fullyVisibleRowCount = async () => {
709+
await expect.poll(() => datatable.bodyHeight()).toBeGreaterThan(0);
710+
await fixture.whenStable();
711+
return Math.floor(datatable.bodyHeight() / component.rowHeight());
712+
};
713+
714+
it('should push enough undefined rows to fill the body when ghost loading with no data', async () => {
707715
component.ghostLoadingIndicator.set(true);
708716
component.scrollbarV.set(true);
709717
component.rows.set([]);
710-
await fixture.whenStable();
718+
const rowCount = await fullyVisibleRowCount();
711719

712720
const internalRows = datatable._internalRows();
713-
const pageSize = datatable.pageSize();
714721
const undefinedCount = internalRows.filter(r => r === undefined).length;
722+
const grid = fixture.nativeElement.querySelector('.datatable-grid') as HTMLElement;
715723

716-
expect(undefinedCount).toBe(Math.max(pageSize, 1));
724+
expect(undefinedCount).toBe(rowCount);
725+
expect(grid.scrollHeight).toBe(grid.clientHeight);
717726
});
718727

719728
it('should push only one undefined row when data already fills the viewport', async () => {
@@ -734,17 +743,16 @@ describe('DatatableComponent With Ghost Loading', () => {
734743
it('should push enough undefined rows to fill remaining viewport when partial data is loaded', async () => {
735744
component.ghostLoadingIndicator.set(true);
736745
component.scrollbarV.set(true);
737-
await fixture.whenStable();
746+
const rowCount = await fullyVisibleRowCount();
738747

739-
const pageSize = datatable.pageSize();
740748
const partialRows = Array.from({ length: 3 }, (_, i) => ({ name: `Row ${i}` }));
741749
component.rows.set(partialRows);
742750
await fixture.whenStable();
743751

744752
const internalRows = datatable._internalRows();
745753
const undefinedCount = internalRows.filter(r => r === undefined).length;
746754

747-
expect(undefinedCount).toBe(Math.max(pageSize - partialRows.length, 1));
755+
expect(undefinedCount).toBe(rowCount - partialRows.length);
748756
expect(internalRows.length).toBe(partialRows.length + undefinedCount);
749757
});
750758

‎projects/ngx-datatable/src/lib/components/datatable.component.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,9 @@ export class DatatableComponent<TRow extends Row = any>
620620
}
621621

622622
if (this.ghostLoadingIndicator() && this.scrollbarV() && !this.externalPaging()) {
623-
const ghostRowCount = Math.max(this.viewportRowCount() - rows.length, 1);
623+
// Only fill the fully visible rows, so that the ghost rows never overflow the body.
624+
const fullyVisibleRowCount = Math.floor(this.bodyHeight() / (this.rowHeight() as number));
625+
const ghostRowCount = Math.max(fullyVisibleRowCount - rows.length, 1);
624626
for (let i = 0; i < ghostRowCount; i++) {
625627
rows.push(undefined);
626628
}

0 commit comments

Comments
 (0)