Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: horizontal scroll #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions projects/ngx-datatable/src/lib/components/datatable.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
OnInit,
Output,
QueryList,
signal,
TemplateRef,
ViewChild,
ViewEncapsulation
Expand Down Expand Up @@ -701,6 +702,11 @@ export class DatatableComponent<TRow = any>
_subscriptions: Subscription[] = [];
_ghostLoadingIndicator = false;
protected verticalScrollVisible = false;
// In case horizontal scroll is enabled
// the column widths are initially calculated without vertical scroll offset
// this makes horizontal scroll to appear on load even if columns can fit in view
// this will be set to true once rows are available and rendered on UI
private _rowInitDone = signal(false);

constructor() {
// apply global settings from Module.forRoot
Expand Down Expand Up @@ -853,6 +859,7 @@ export class DatatableComponent<TRow = any>
}
if (rowDiffers) {
queueMicrotask(() => {
this._rowInitDone.set(true);
this.recalculate();
this.cd.markForCheck();
});
Expand Down Expand Up @@ -904,34 +911,27 @@ export class DatatableComponent<TRow = any>
}
const bodyElement = this.bodyElement?.nativeElement;
this.verticalScrollVisible = bodyElement?.scrollHeight > bodyElement?.clientHeight;
if (this.scrollbarV && !this.scrollbarVDynamic) {
width = width - (this.verticalScrollVisible ? this.scrollbarHelper.width : 0);
} else if (this.scrollbarVDynamic) {
const scrollerHeight = this.bodyComponent?.scroller?.element.offsetHeight;
if (scrollerHeight && this.bodyHeight < scrollerHeight) {
width = width - (this.verticalScrollVisible ? this.scrollbarHelper.width : 0);
}

if (this.headerComponent && this.headerComponent.innerWidth !== width) {
this.headerComponent.innerWidth = width;
}
if (this.bodyComponent && this.bodyComponent.innerWidth !== width) {
this.bodyComponent.innerWidth = width;
this.bodyComponent.cd.markForCheck();
}
if (this.scrollbarV || this.scrollbarVDynamic) {
width =
width -
(this.verticalScrollVisible || !this._rowInitDone() ? this.scrollbarHelper.width : 0);
}

if (this.columnMode === ColumnMode.force) {
forceFillColumnWidths(columns, width, forceIdx, allowBleed);
forceFillColumnWidths(columns, width, forceIdx, allowBleed, 300, this.scrollbarHelper.width);
} else if (this.columnMode === ColumnMode.flex) {
adjustColumnWidths(columns, width);
}

if (this.bodyComponent) {
this.bodyComponent.updateColumnGroupWidths();
if (this.bodyComponent && this.bodyComponent.columnGroupWidths.total !== width) {
this.bodyComponent.columns = [...this._internalColumns];
this.bodyComponent.cd.markForCheck();
}

if (this.headerComponent && this.headerComponent._columnGroupWidths.total !== width) {
this.headerComponent.columns = [...this._internalColumns];
}

return columns;
}

Expand Down
7 changes: 5 additions & 2 deletions projects/ngx-datatable/src/lib/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ export function forceFillColumnWidths(
expectedWidth: number,
startIdx: number,
allowBleed: boolean,
defaultColWidth: number = 300
defaultColWidth: number = 300,
verticalScrollWidth = 0
) {
const columnsToResize = allColumns
.slice(startIdx + 1, allColumns.length)
Expand All @@ -142,6 +143,7 @@ export function forceFillColumnWidths(
let exceedsWindow = false;
let contentWidth = getContentWidth(allColumns, defaultColWidth);
let remainingWidth = expectedWidth - contentWidth;
const initialRemainingWidth = remainingWidth;
const columnsProcessed: any[] = [];
const remainingWidthLimit = 1; // when to stop

Expand All @@ -151,7 +153,8 @@ export function forceFillColumnWidths(
exceedsWindow = contentWidth >= expectedWidth;

for (const column of columnsToResize) {
if (exceedsWindow && allowBleed) {
// don't bleed if the initialRemainingWidth is same as verticalScrollWidth
if (exceedsWindow && allowBleed && initialRemainingWidth !== -1 * verticalScrollWidth) {
column.width = column.$$oldWidth || column.width || defaultColWidth;
} else {
const newSize = (column.width || defaultColWidth) + additionWidthPerColumn;
Expand Down