Skip to content

Commit 248c3a7

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.
1 parent 74f96d3 commit 248c3a7

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
@@ -670,7 +670,10 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
670670
// that shows up inside the view port the last.
671671
const height = parseInt(this._bodyHeight(), 10);
672672
first = this.rowHeightsCache().getRowIndex(this.offsetY());
673-
last = this.rowHeightsCache().getRowIndex(height + this.offsetY()) + 1;
673+
last = Math.min(
674+
this.rowHeightsCache().getRowIndex(height + this.offsetY()) + 1,
675+
this.controller.rowCount()
676+
);
674677
}
675678
} else {
676679
// 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
@@ -786,6 +786,7 @@ describe('DatatableComponent With Ghost Loading', () => {
786786
imports: [DatatableComponent],
787787
template: `
788788
<ngx-datatable
789+
style="block-size: 100%"
789790
columnMode="force"
790791
[rows]="rows()"
791792
[columns]="columns()"
@@ -821,17 +822,25 @@ describe('DatatableComponent With Ghost Loading', () => {
821822
datatable = fixture.debugElement.query(By.directive(DatatableComponent)).componentInstance;
822823
});
823824

824-
it('should push enough undefined rows to fill pageSize when ghost loading with no data', async () => {
825+
/** Waits for the body to be measured and returns the number of rows fitting into it completely. */
826+
const fullyVisibleRowCount = async () => {
827+
await expect.poll(() => datatable.bodyHeight()).toBeGreaterThan(0);
828+
await fixture.whenStable();
829+
return Math.floor(datatable.bodyHeight() / component.rowHeight());
830+
};
831+
832+
it('should push enough undefined rows to fill the body when ghost loading with no data', async () => {
825833
component.ghostLoadingIndicator.set(true);
826834
component.scrollbarV.set(true);
827835
component.rows.set([]);
828-
await fixture.whenStable();
836+
const rowCount = await fullyVisibleRowCount();
829837

830838
const internalRows = datatable._internalRows();
831-
const pageSize = datatable.pageSize();
832839
const undefinedCount = internalRows.filter(r => r === undefined).length;
840+
const grid = fixture.nativeElement.querySelector('.datatable-grid') as HTMLElement;
833841

834-
expect(undefinedCount).toBe(Math.max(pageSize, 1));
842+
expect(undefinedCount).toBe(rowCount);
843+
expect(grid.scrollHeight).toBe(grid.clientHeight);
835844
});
836845

837846
it('should push only one undefined row when data already fills the viewport', async () => {
@@ -852,17 +861,16 @@ describe('DatatableComponent With Ghost Loading', () => {
852861
it('should push enough undefined rows to fill remaining viewport when partial data is loaded', async () => {
853862
component.ghostLoadingIndicator.set(true);
854863
component.scrollbarV.set(true);
855-
await fixture.whenStable();
864+
const rowCount = await fullyVisibleRowCount();
856865

857-
const pageSize = datatable.pageSize();
858866
const partialRows = Array.from({ length: 3 }, (_, i) => ({ name: `Row ${i}` }));
859867
component.rows.set(partialRows);
860868
await fixture.whenStable();
861869

862870
const internalRows = datatable._internalRows();
863871
const undefinedCount = internalRows.filter(r => r === undefined).length;
864872

865-
expect(undefinedCount).toBe(Math.max(pageSize - partialRows.length, 1));
873+
expect(undefinedCount).toBe(rowCount - partialRows.length);
866874
expect(internalRows.length).toBe(partialRows.length + undefinedCount);
867875
});
868876

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

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

637637
if (this.ghostLoadingIndicator() && this.scrollbarV() && !this.externalPaging()) {
638-
const ghostRowCount = Math.max(this.viewportRowCount() - rows.length, 1);
638+
// Only fill the fully visible rows, so that the ghost rows never overflow the body.
639+
const fullyVisibleRowCount = Math.floor(this.bodyHeight() / (this.rowHeight() as number));
640+
const ghostRowCount = Math.max(fullyVisibleRowCount - rows.length, 1);
639641
for (let i = 0; i < ghostRowCount; i++) {
640642
rows.push(undefined);
641643
}

0 commit comments

Comments
 (0)