Hello everyone,
I have a question regarding the eventListener function in dash_ag_grid. I am working with a table that features multiple hierarchical column groups, each initialized with either "columnGroupShow": "closed"
or "columnGroupShow": "open"
, so that users can toggle the visibility of the columns. When a user opens or closes a column group, I would like to trigger a callback that indicates which columns (from my columnDefs
) are currently visible.
According to the Grid Events Reference (React Grid: Grid Events Reference | AG Grid), the ColumnGroupOpenedEvent
should provide the functionality I need. I have also reviewed the examples on Dash AG Grid Event Listeners (v 31.2), but as I am relatively new to Dash, I have not yet been able to implement a similar event listener to capture the column group state.
Any help or guidance would be greatly appreciated. I can provide an example of how the column groups are set up:
import dash
from dash import html, Output, Input, dcc, State
import dash_ag_grid as dag
app = dash.Dash(__name__)
columnDefs = [
{
"headerName": "Group A",
"children": [
{
"field": "col1",
"headerName": "Column 1",
"columnGroupShow": "closed" # closed if Column 2 is open
},
{
"field": "col2",
"headerName": "Column 2", # closed if Column 1 is open
"columnGroupShow": "open"
}
]
},
{
"headerName": "Group B",
"children": [
{
"headerName": "Subgroup B1",
"children": [
{
"field": "col3",
"headerName": "Column 3",
"columnGroupShow": "closed" # closed if Column 4 is open
},
{
"field": "col4",
"headerName": "Column 4",
"columnGroupShow": "open" # closed if Column 3 is open
}
]
},
{
"field": "col5",
"headerName": "Column 5" # always opened
}
]
}
]
rowData = [
{"col1": "A1", "col2": "A2", "col3": "B1", "col4": "B2", "col5": "B3"},
{"col1": "A3", "col2": "A4", "col3": "B4", "col4": "B5", "col5": "B6"},
]
app.layout = html.Div([
dag.AgGrid(
id="grid",
columnDefs=columnDefs,
rowData=rowData,
),
html.Div(id='output_div', children=[]),
dcc.Store(id='opened_column_groups', data=[])
])
@app.callback(
Output('opened_column_groups', 'data'),
Output('output_div', 'children'),
Input('output_div', 'children'), # This should be the Event triggered by an opened columnGroup
State('opened_column_groups', 'data'),
)
def update_column_groups_opened(children, data):
opened_groups = []
# get name of opened columns and add them to opened_groups
return opened_groups, f"Opened Column Groups: {opened_groups}"
if __name__ == '__main__':
app.run_server(debug=True)
Thank you in advance!