Skip to content

Commit 3177551

Browse files
authored
fix: column header reorder should keep scroll position even frozen grid (#1148)
* fix: column header reorder should keep scroll position even frozen grid
1 parent e39304a commit 3177551

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

src/slick.grid.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
19771977
onEnd: (e: SortableEvent) => {
19781978
e.item.classList.remove('slick-header-column-active');
19791979
clearInterval(columnScrollTimer);
1980+
const prevScrollLeft = this.scrollLeft;
19801981

19811982
if (!this.getEditorLock()?.commitCurrentEdit()) {
19821983
return;
@@ -1993,6 +1994,8 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
19931994
e.stopPropagation();
19941995
if (!this.arrayEquals(prevColumnIds, reorderedIds)) {
19951996
this.setColumns(reorderedColumns);
1997+
// reapply previous scroll position since it might move back to x=0 after calling `setColumns()` (especially when `frozenColumn` is set)
1998+
this.scrollToX(prevScrollLeft);
19961999
this.trigger(this.onColumnsReordered, { impactedColumns: this.columns, previousColumnOrder: prevColumnIds });
19972000
this.setupColumnResize();
19982001
}
@@ -6726,34 +6729,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
67266729
this.prevScrollLeft = this.scrollLeft;
67276730

67286731
// adjust scroll position of all div containers when scrolling the grid
6729-
this._viewportScrollContainerX.scrollLeft = this.scrollLeft;
6730-
this._headerScrollContainer.scrollLeft = this.scrollLeft;
6731-
this._topPanelScrollers[0].scrollLeft = this.scrollLeft;
6732-
if (this._options.createFooterRow) {
6733-
this._footerRowScrollContainer.scrollLeft = this.scrollLeft;
6734-
}
6735-
if (this._options.createPreHeaderPanel) {
6736-
if (this.hasFrozenColumns()) {
6737-
this._preHeaderPanelScrollerR.scrollLeft = this.scrollLeft;
6738-
} else {
6739-
this._preHeaderPanelScroller.scrollLeft = this.scrollLeft;
6740-
}
6741-
}
6742-
if (this._options.createTopHeaderPanel) {
6743-
this._topHeaderPanelScroller.scrollLeft = this.scrollLeft;
6744-
}
6745-
6746-
if (this.hasFrozenColumns()) {
6747-
if (this.hasFrozenRows) {
6748-
this._viewportTopR.scrollLeft = this.scrollLeft;
6749-
}
6750-
this._headerRowScrollerR.scrollLeft = this.scrollLeft; // right header row scrolling with frozen grid
6751-
} else {
6752-
if (this.hasFrozenRows) {
6753-
this._viewportTopL.scrollLeft = this.scrollLeft;
6754-
}
6755-
this._headerRowScrollerL.scrollLeft = this.scrollLeft; // left header row scrolling with regular grid
6756-
}
6732+
this.scrollToX(this.scrollLeft);
67576733
}
67586734

67596735
// autoheight suppresses vertical scrolling, but editors can create a div larger than
@@ -7772,6 +7748,41 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
77727748
return Array.isArray(arr1) && Array.isArray(arr2) && arr2.toString() === arr1.toString();
77737749
}
77747750

7751+
/**
7752+
* Scroll to an X coordinate position in the grid
7753+
* @param {Number} x
7754+
*/
7755+
scrollToX(x: number): void {
7756+
this._viewportScrollContainerX.scrollLeft = x;
7757+
this._headerScrollContainer.scrollLeft = x;
7758+
this._topPanelScrollers[0].scrollLeft = x;
7759+
if (this._options.createFooterRow) {
7760+
this._footerRowScrollContainer.scrollLeft = x;
7761+
}
7762+
if (this._options.createPreHeaderPanel) {
7763+
if (this.hasFrozenColumns()) {
7764+
this._preHeaderPanelScrollerR.scrollLeft = x;
7765+
} else {
7766+
this._preHeaderPanelScroller.scrollLeft = x;
7767+
}
7768+
}
7769+
if (this._options.createTopHeaderPanel) {
7770+
this._topHeaderPanelScroller.scrollLeft = x;
7771+
}
7772+
7773+
if (this.hasFrozenColumns()) {
7774+
if (this.hasFrozenRows) {
7775+
this._viewportTopR.scrollLeft = x;
7776+
}
7777+
this._headerRowScrollerR.scrollLeft = x; // right header row scrolling with frozen grid
7778+
} else {
7779+
if (this.hasFrozenRows) {
7780+
this._viewportTopL.scrollLeft = x;
7781+
}
7782+
this._headerRowScrollerL.scrollLeft = x; // left header row scrolling with regular grid
7783+
}
7784+
}
7785+
77757786
/**
77767787
* Converts a value to a string and escapes HTML characters (&, <, >). Returns an empty string if the value is not defined.
77777788
*/

0 commit comments

Comments
 (0)