Skip to content

Commit d513f3e

Browse files
authored
Merge pull request #408 from BSd3v/fix-columndef-state-issue
2 parents beb8003 + 0e7b159 commit d513f3e

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to `dash-ag-grid` will be documented in this file.
44
This project adheres to [Semantic Versioning](https://semver.org/).
55
Links "DE#nnn" prior to version 2.0 point to the Dash Enterprise closed-source Dash AG Grid repo
66

7+
## [unreleased]
8+
### Fixed
9+
- [#408](https://github.com/plotly/dash-ag-grid/pull/408) fixed issue where the `columnState` would conflict with `columnDefs` updates
10+
- fixes [#416] (https://github.com/plotly/dash-ag-grid/issues/416)
11+
- fixes [#407](https://github.com/plotly/dash-ag-grid/issues/407)
12+
713
## [33.3.2rc2] - 2025-09-17
814

915
### Fixed

src/lib/fragments/AgGrid.react.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,10 +1365,7 @@ export function DashAgGrid(props) {
13651365

13661366
// Handle columnState push changes
13671367
useEffect(() => {
1368-
if (
1369-
gridApi &&
1370-
(!props.loading_state || prevProps?.loading_state?.is_loading)
1371-
) {
1368+
if (gridApi && !(props.dashRenderType === 'internal')) {
13721369
const existingColumnState = gridApi.getColumnState();
13731370
const realStateChange =
13741371
props.columnState &&
@@ -1632,10 +1629,17 @@ const MemoizedAgGrid = React.memo(DashAgGrid, (prevProps, nextProps) => {
16321629
nextProps.selectedRows,
16331630
prevProps.selectedRows
16341631
);
1632+
const columnStateChanged = !equals(
1633+
nextProps.columnState,
1634+
prevProps.columnState
1635+
);
16351636

16361637
if (
16371638
propsHaveChanged &&
1638-
(!isInternalChange || rowDataChanged || selectedRowsChanged)
1639+
(!isInternalChange ||
1640+
rowDataChanged ||
1641+
selectedRowsChanged ||
1642+
columnStateChanged)
16391643
) {
16401644
return false; // Props changed, re-render
16411645
}

tests/test_column_state.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from dash import Dash, html, Output, Input, no_update, State, ctx, Patch
1+
from dash import Dash, html, Output, Input, no_update, State, ctx, Patch, dcc
22
import dash_ag_grid as dag
33
import plotly.express as px
44
import json
55
import time
6+
import pandas as pd
67

78
from . import utils
89
from dash.testing.wait import until
@@ -518,3 +519,70 @@ def remove_column(n):
518519
dash_duo.find_element("#remove-column").click()
519520
time.sleep(2) # pausing to emulate separation because user inputs
520521
assert list(filter(lambda i: i.get("level") != "ERROR", dash_duo.get_logs())) == []
522+
523+
def test_toggle_column_visibility(dash_duo):
524+
data = pd.DataFrame([
525+
{"a": 1, "b": 2},
526+
{"a": 3, "b": 4},
527+
])
528+
529+
app = Dash(__name__)
530+
531+
app.layout = html.Div([
532+
dcc.Dropdown(
533+
id="select-columns",
534+
value=list(data.columns),
535+
options=[{"label": col, "value": col} for col in data.columns],
536+
multi=True,
537+
),
538+
dag.AgGrid(
539+
id="ag-grid",
540+
style={"height": "75vh", "width": "100%"},
541+
rowData=data.to_dict(orient="records"),
542+
),
543+
])
544+
545+
@app.callback(
546+
Output("ag-grid", "columnDefs"),
547+
Input("select-columns", "value"),
548+
)
549+
def toggle_column_visibility(selected_columns):
550+
if not selected_columns:
551+
return no_update
552+
return [
553+
{
554+
"headerName": col_name,
555+
"field": col_name,
556+
"hide": col_name not in selected_columns,
557+
}
558+
for col_name in data.columns
559+
]
560+
561+
dash_duo.start_server(app)
562+
563+
# Wait for grid to render
564+
grid = utils.Grid(dash_duo, "ag-grid")
565+
566+
grid.wait_for_cell_text(0, 0, "1")
567+
568+
# Hide column 'b'
569+
dropdown = dash_duo.find_element("#select-columns")
570+
option_b = dash_duo.find_element('span.Select-value-icon:nth-child(1)')
571+
option_b.click()
572+
time.sleep(1)
573+
574+
# Only column 'a' should be visible
575+
grid_headers = dash_duo.find_elements("div.ag-header-cell-label")
576+
header_texts = [h.text for h in grid_headers]
577+
assert "a" not in header_texts
578+
assert "b" in header_texts
579+
580+
# Show both columns again
581+
dropdown.click()
582+
option_b = dash_duo.find_element('.Select-menu')
583+
option_b.click()
584+
time.sleep(1)
585+
grid_headers = dash_duo.find_elements("div.ag-header-cell-label")
586+
header_texts = [h.text for h in grid_headers]
587+
assert "a" in header_texts
588+
assert "b" in header_texts

0 commit comments

Comments
 (0)