Skip to content

DataGrid: Fix PageUp/PageDown focusing a row from the previous scroll position when rows render asynchronously - #36298

Open
Alyar666 wants to merge 6 commits into
DevExpress:mainfrom
Alyar666:page_up_down_async_render_26_2
Open

Alyar666 wants to merge 6 commits into
DevExpress:mainfrom
Alyar666:page_up_down_async_render_26_2

Conversation

@Alyar666

Copy link
Copy Markdown
Contributor

No description provided.

@Alyar666
Alyar666 requested a review from a team as a code owner November 16, 2026 08:22
@Alyar666 Alyar666 added the 26_2 label Nov 16, 2026
@Alyar666 Alyar666 self-assigned this Nov 16, 2026
Copilot AI balanced review requested due to automatic review settings November 16, 2026 08:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Asynchronous page navigation still mishandles focus restoration during rapid direction reversals and legacy virtual scrolling, leaving focus stuck on the rows container or restoring it against stale rows.

2 open issues
What changed in this PR

This PR updates shared grid keyboard navigation to defer PageUp/PageDown focus restoration until asynchronous viewport loading and rendering finish.

Changes:

  • Exposes pending viewport and row-rendering state through the scrolling controllers and rows view.
  • Defers page-navigation focus updates and cancels pending restoration when a row is clicked.
  • Adds integration tests for asynchronous rendering, remote loading, templates, and keyboard interactions.
File Description
packages/​devextreme/​js/​__internal/​grids/​grid_core/​virtual_scrolling/​virtual_scrolling_core.ts Tracks pending viewport timeouts and clears their state consistently.
packages/​devextreme/​js/​__internal/​grids/​grid_core/​virtual_scrolling/​extenders/​virtual_scrolling_data_controller.ts Exposes the rows scroll controller’s pending state.
packages/​devextreme/​js/​__internal/​grids/​grid_core/​views/​rows_view.ts Combines loading, viewport, and template readiness checks.
packages/​devextreme/​js/​__internal/​grids/​grid_core/​keyboard_navigation/​keyboard_navigation.ts Defers focus restoration while page navigation is pending.
packages/​devextreme/​js/​__internal/​grids/​grid_core/​keyboard_navigation/​__tests__/​page_navigation.integration.test.ts Adds page-navigation regression coverage.
packages/​devextreme/​js/​__internal/​grids/​grid_core/​data_controller/​data_controller.ts Adds the base pending-viewport query for scrolling extensions.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 16, 2026 09:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Virtual-grid PageUp/PageDown navigation can leave focus on the previous page because custom loading blocks focus restoration, which is not retried when custom loading ends.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Custom loading blocks virtual grid page navigation focus restoration

packages/​devextreme/​js/​__internal/​grids/​grid_core/​views/​rows_view.ts:526

isLoading() includes the flag set by beginCustomLoading(), so PageUp/PageDown during custom loading in a virtual grid (for example, with loadPanel.enabled: false) leaves page navigation pending even after the viewport renders. endCustomLoading() only emits loadingChanged, so focus restoration is not retried and the focused row position stays on the previous page until another render. Distinguish actual pending viewport work from the custom loading indicator, while preserving checks for simultaneous real loading, and add a regression test for this sequence.

2 resolved since last review

Review effort: Balanced

@Alyar666
Alyar666 force-pushed the page_up_down_async_render_26_2 branch from f99f319 to 46b2649 Compare November 16, 2026 11:39
Copilot AI balanced review requested due to automatic review settings November 16, 2026 11:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Virtual-grid PageUp/PageDown navigation can leave the focused-row index stale after target rows render because indicator-only loading blocks viewport readiness and ending it does not trigger another render.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 16, 2026 15:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because pending page-navigation rendering can override explicit focus changes or resets.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@Alyar666
Alyar666 force-pushed the page_up_down_async_render_26_2 branch from d70b689 to 270250e Compare November 16, 2026 20:43
Copilot AI balanced review requested due to automatic review settings November 16, 2026 20:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Pending page navigation can restore focus to the previous row and undo scrolling, or overwrite an explicit grid.focus(cell) selection when rendering completes.

2 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 16, 2026 23:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed to evaluate an asynchronous page-navigation race that can undo an explicit focused-row reset.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

return this._rowsView.getTopVisibleItemIndex() + this._dataController.getRowIndexOffset();
}

private completePageNavigation(): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only runs from renderCompleted, so if the viewport update ends without a rows render, the flag stays set and _focusedCellPosition keeps the row from before PageDown.

Example: remote store, scrolling.mode: 'virtual', renderAsync: true, PageDown to a page that isn't cached, and the load fails. DataSourceAdapter.loadErrorHandler fires changed with changeType: 'loadError', and RowsView._handleDataChanged sends it to _update(), which doesn't render (only the focus and selection extenders handle their own change types there). So renderCompleted never fires. Once loading ends, keys are no longer blocked, and the next ArrowDown moves from the old row, which is the symptom this PR fixes. Before this change, restoreFocusableElement always set the position immediately.

This is a different path from the PageDown → PageUp reversal discussed above. Could completion be tied to the end of the viewport update itself (rendered, no-op, or failed) instead of the next renderCompleted?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right: after a failed load the flag stays set and the position keeps the row from before PageDown until the next render.

The problem is that there is no correct row to complete with. A failed load doesn't re-render, so the DOM still has the old rows while the scroll position and the row index offset are already new. I tried completing the navigation on dataErrorOccurred: the pick landed on row 15, which isn't rendered, and arrows still did nothing. Base without renderAsync behaves the same after a failed load: focus stays on the rows view and arrows do nothing.

The "no-op" case doesn't occur: after the scroll timeout a render always follows (partialUpdateItem returns an update for every kept row, so cancelEmptyChanges doesn't cancel it).

After the error, the next successful render (retry, scroll, refresh) completes the navigation and focuses the top visible row. Any focus change (a click, the API) now drops the flag too (see the setFocusedRowIndex thread). So I'd leave the error case as is in this PR, unless you see a better target row for it.

return false;
}

return this.dataSourceController.isLoading()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dataSourceController.isLoading() is also true during CustomLoader loads. CustomLoader.load calls dataSource._scheduleLoadCallbacks, which increments the loading count, and in non-legacy virtual mode the adapter's isLoading() returns _dataSource.isLoading(). Callers include selection (selection.ts), export (loadAll), header filter values and the focused-row index lookup (focus.ts). None of these loads is followed by a rows render.

Example: remote virtual grid, PageDown while a select-all or focused-row lookup load is running. The viewport render's renderCompleted sees isLoading() === true and keeps the navigation pending (keys stay blocked meanwhile). When the custom load finishes, nothing renders, so the focused position stays at the row from before PageDown until some unrelated render.

virtual_scrolling.ts:146 already excludes these loads: this._dataSource.isLoading() && !this.customLoader.isLoading(). This is separate from the beginCustomLoading case in the earlier thread.

}

this.isPageNavigationPending = false;
this.setFocusedCellPosition(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up to the grid.focus() thread: clearing the flag in _focusElement and in the success branch of _clickTargetCellHandler covers those two paths. Other focus changes made while navigation is pending still get replaced with the top visible row here:

  • a click in a master-detail cell or in the free-space row: _pointerEventHandler calls _updateFocusedCellPosition directly;
  • args.resetFocusedRow in _clickTargetCellHandler: it returns before the flag is cleared, so the reset row is focused again on completion.

The pending window is the scroll timeout plus the whole remote load, so a click in master-detail during a slow load is realistic. Because the focus extender's setFocusedRowIndex writes the focusedRowIndex option, this also changes focusedRowKey and raises onFocusedRowChanged. I haven't traced whether option('focusedRowKey', ...) / navigateToRow() while pending end up in _focusElement. If they don't, they're affected too.

Since every new focus path has to remember to clear the flag, could the pending state be dropped whenever the focused position changes from anywhere other than this completion?

private isWaitingForRowsRendering(eventArgs: KeyboardKeyDownEvent): boolean {
return this._rowsView.isWaitingForRowsLayout()
const isWaitingForRows = this._rowsView.isWaitingForRowsLayout()
|| (this.isPageNavigationPending && this._rowsView.isViewportRowsRenderPending());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this condition, every key in FOCUSED_CELL_KEYS (including tab, enter, space, home/end, see const.ts:38) gets preventDefault() and stopPropagation() for the whole remote load after PageDown. Before, keys were blocked only during async template layout, which is short.

With a slow or hanging request, a keyboard user can't Tab out of the grid until the load ends, and Enter/Space presses are dropped, not queued. Could Tab/Shift+Tab at least be allowed to leave the grid while page navigation is pending, or the wait be limited to the keys that depend on the focused row position?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, with a slow request this is a long wait. I compared it with base: Tab during the same wait isn't better there. It runs with the stale row picked before the new rows arrived, so it doesn't reach the right cell either. With this PR, Tab is ignored until the load ends, and then focus is on the correct row. PageUp/PageDown, Ctrl+Up/Down, Escape and the mouse still work during the wait.

Letting Tab through isn't simple. The grid's Tab handler would move from the row before PageDown, which is scrolled away. A native Tab from the focused rows view can land on a cell or editor of the stale rows. Both need a browser check, and neither looks better than waiting. I'd keep this PR as is and handle "Tab out of the grid during a long load" in a separate task if we want it. What do you think?

/**
* @extended: virtual_scrolling
*/
public isViewportPositionPending(): boolean {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method placement: the pending timer lives in VirtualScrollController, and the data controller is the one that decides which controller gets the position (_rowsScrollController or the adapter, in setViewportPosition). So the data controller is the right owner of this query, but it doesn't need a stub in the base DataController. The stub is only here because the caller, isViewportRowsRenderPending, is in the base RowsView and sees the base type.

Suggestion:

  • declare isViewportPositionPending in VirtualScrollingDataControllerExtension (virtual_scrolling/types.ts), next to setViewportPosition, and remove this stub;
  • move the body of isViewportRowsRenderPending (including the isVirtualPaging || isVirtualRowRendering check) into VirtualScrollingRowsViewExtender, which already types _dataController as DataController & VirtualScrollingDataControllerExtension;
  • keep only a return false; default in the base RowsView, so keyboard navigation doesn't depend on virtual-scrolling types.

This way the base RowsView and DataController don't need to know about the virtual-scrolling timer.

if (!args.cancel && args.rowIndexChanged) {
newRowIndex = args.newRowIndex;
}
this.isPageNavigationPending = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, and not a regression (the old code had the same gap): the flag is set only here. This code runs from _scrollBy, after the scrollable's next scroll event and then a setTimeout. Between PageDown and this callback, isWaitingForRowsRendering doesn't block anything. So an ArrowDown pressed right after PageDown moves from the old row and scrolls it back into view, undoing the page scroll.

Setting the flag when the PageUp/PageDown scroll starts would close most of that window. If no scroll event follows (for example, the grid is already scrolled to the boundary), the flag would need to be reset there, or it would stay pending.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this window exists in base too. I checked that ArrowDown pressed between PageDown and the scroll callback behaves the same with and without this PR: in both, the completion then moves focus to the new top row.

To set the flag on the key press, we'd need a reliable "no scroll will happen" signal (already at the top or bottom, or the scrollable clamps the distance). Otherwise the flag stays set and a later render overwrites the user's focus. I'd keep it out of this PR and can file a follow-up if you think it's worth it.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 13:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants