Skip to content

Fix auto export row selection sync by using correct AG Grid event#81

Open
jlancaster7 wants to merge 1 commit intowidgetti:masterfrom
jlancaster7:fix/auto-export-selection-sync
Open

Fix auto export row selection sync by using correct AG Grid event#81
jlancaster7 wants to merge 1 commit intowidgetti:masterfrom
jlancaster7:fix/auto-export-selection-sync

Conversation

@jlancaster7
Copy link

Problem

When export_mode='auto' is set, clicking a row in the grid should sync the selected row data to Python via grid_data_out and fire observe callbacks. Instead, grid_data_out stays empty and no callbacks fire. The grid renders fine and selection highlights visually, but nothing reaches Python.

Reported in #68 and #76.

Root Cause

In js/src/widget_builder.js, the auto export listener for row selection was using AG Grid's rowSelected event instead of selectionChanged:

  • rowSelected fires once per affected row during a selection change. In single-selection mode, clicking a new row fires it twice (deselect old + select new). The first firing occurs mid-transition when getSelectedNodes() returns inconsistent state. Both firings call model.set('_grid_data_up', ...) in rapid succession, and traitlets silently drops the update because it doesn't detect a meaningful change.

  • selectionChanged fires once after all row selection/deselection is complete. At that point, getSelectedNodes() returns the final, consistent selection state. This is the correct event per AG Grid documentation.

Fix

One-line change in js/src/widget_builder.js:180:

-            gridOptions.api.addEventListener('rowSelected', () => {
+            gridOptions.api.addEventListener('selectionChanged', () => {

Testing

Verified in a Jupyter notebook with the following test cases:

  • export_mode='auto' + rowSelection: 'single' — clicking a row fires observe callback on grid_data_out immediately
  • export_mode='auto' + rowSelection: 'multiple' — Ctrl+click selects multiple rows, all appear in grid_data_out['rows']
  • export_mode='auto' + export_to_df=True — selected rows returned as DataFrame
  • export_mode='auto' + export_to_df=False — selected rows returned as list of dicts
  • export_mode='buttons' — still works (no regression)
  • get_selected_rows() — still works (no regression)

Related Issues

Change 'rowSelected' to 'selectionChanged' in widget_builder.js.

The auto export mode was listening to AG Grid's 'rowSelected' event,
which fires per individual row toggle and causes inconsistent selection
state during rapid successive updates. The correct event is
'selectionChanged', which fires once after the overall selection state
is settled and is the standard event to pair with api.getSelectedRows().

This fixes export_mode='auto' not syncing selected row data from
JS to Python via _grid_data_up / grid_data_out.

Fixes widgetti#68
Fixes widgetti#76
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant