Configure the grid to allow users to select rows by clicking them, focusing a row and pressing <kbd>␣ Space<kbd> or via
API.
Row selection is configured with the rowSelection grid Option. Setting rowSelection.mode to either 'multiRow' or
'singleRow' will allow you to select rows by clicking, focusing a row and pressing <kbd>␣ Space<kbd> or via the
selection API.
dashGridOptions = {
'rowSelection': {
'mode': 'singleRow',
},
# other grid options...
}
See detailed documentation on the two row selection modes:
The following example illustrates a basic row selection configuration. Use the select control to choose the default
single row or multi-row configuration.
```python
import dash_ag_grid as dag
from dash import Dash, html, dcc, Input, Output, callback
import pandas as pd
app = Dash()
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/ag-grid/olympic-winners.csv"
)
columnDefs = [{"field": i} for i in ["country", "year", "athlete", "age", "sport", "total"]]
app.layout = html.Div(
[
dcc.RadioItems(
id='radio-row-selection-overview',
options={'singleRow': 'Single Row Selection', 'multiRow': 'Multi Row Selection'},
value='singleRow'
),
html.Pre(id="pre-row-selection-overview"),
dag.AgGrid(
id="grid-row-selection-overview",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="sizeToFit",
),
],
)
@callback(
Output("grid-row-selection-overview", "dashGridOptions"),
Output("pre-row-selection-overview", "children"),
Input('radio-row-selection-overview', "value"),
)
def set_grid_options(row_selection):
grid_options = {
"rowSelection": {'mode': row_selection},
}
output = {k: v for k, v in grid_options.items() if v is not False}
return grid_options, f'dashGridOptions = {output}'
if __name__ == "__main__":
app.run(debug=True)
```
Row selection can be used when using row grouping, tree data and the server-side row model. See the respective sections
of the documentation:
See AG Grid docs API Reference for the full list of
configuration options available for the row selection.
<br>