Enable users to select a single row within a grid.
To enable single row selection set rowSelection.mode to 'singleRow'.
dashGridOptions = {
'rowSelection': {
'mode': 'singleRow',
},
# other grid options...
}
The example below uses this configuration to restrict selection to a single row.
Deselect a row by clicking its checkbox. Alternatively, you can do this via the keyboard by focusing the row and
pressing the <kbd>␣ Space<kbd> key.
```python
import dash_ag_grid as dag
from dash import Dash
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 = dag.AgGrid(
id="grid-single-row-selection-simple",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="sizeToFit",
dashGridOptions={
"rowSelection": {'mode': 'singleRow'},
}
)
if __name__ == "__main__":
app.run(debug=True)
```
The rowSelection.enableClickSelection property configures whether a row’s selection state will be impacted when the
row is clicked.
enableClickSelection:”>https://www.ag-grid.com/archive/32.3.2/react-data-grid//row-selection-single-row/#reference-SingleRowSelectionOptions-enableClickSelection):This is typically used
when Checkbox Selection is disabled, though
both can be enabled simultaneously if desired. Click-selection and deselection can be enabled by setting
enableClickSelection to True, otherwise they may be enabled separately using the values 'enableSelection' and
'enableDeselection'.
dashGridOptions = {
'rowSelection': {
'mode': 'singleRow',
'enableClickSelection': True,
},
# other grid options...
}
The example below demonstrates the three possible configurations for this property, as well as the behavior when it is
disabled. Click a row to select it, and <kbd>Ctrl<kbd>-click to deselect it.
Note that deselection is still possible using the <kbd>␣ Space<kbd> key or when checkboxes are enabled by clicking a
selected checkbox.
<br>
```python
import dash_ag_grid as dag
from dash import Dash, html, dcc, Input, Output, callback
import pandas as pd
import json
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(
[
"'enableClickSelection' Value:",
dcc.RadioItems(
id='radio-single-row-selection-click-selection',
options=['True', 'enableSelection', 'enableDeselection', 'False (Default)'],
value='True',
inline=True
),
dag.AgGrid(
id="grid-single-row-selection-click-selection",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="sizeToFit",
dashGridOptions={"rowSelection": {'mode': 'singleRow'}}
),
html.Pre(id="pre-single-row-selection-click-selection")
],
)
@callback(
Output("grid-single-row-selection-click-selection", "dashGridOptions"),
Output("pre-single-row-selection-click-selection", "children"),
Input("radio-single-row-selection-click-selection", "value"),
)
def click_selection_options(selected_value):
if selected_value == 'True':
selected_value = True
grid_options: dict = {"rowSelection": {'mode': 'singleRow'}}
# No need to set to False as it is the default value
if selected_value != 'False (Default)':
grid_options["rowSelection"]['enableClickSelection'] = selected_value
formatted_output = json.dumps(grid_options, indent=4).replace("true", "True")
return grid_options, f'dashGridOptions = {formatted_output}'
if __name__ == "__main__":
app.run(debug=True)
```
To prevent any row selection checkboxes from being rendered, set rowSelection.checkboxes to False.
Selection is still possible using the <kbd>␣ Space<kbd> key, or setting enableClickSelection to select clicking on
the row.
dashGridOptions = {
'rowSelection': {
'mode': 'singleRow',
'checkboxes': False,
'enableClickSelection': True,
},
# other grid options...
}
```python
import dash_ag_grid as dag
from dash import Dash
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 = dag.AgGrid(
id="grid-single-row-selection-remove-checkboxes",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="sizeToFit",
dashGridOptions={
"rowSelection": {
'mode': 'singleRow',
'checkboxes': False,
'enableClickSelection': True,
},
}
)
if __name__ == "__main__":
app.run(debug=True)
```
You may also pass a function to rowSelection.checkboxes to dynamically enable or disable checkboxes for given rows.
For example, to enable checkboxes for rows where the ‘year’ property is less than 2007, we can set:
dashGridOptions = {
'rowSelection': {
'mode': 'singleRow',
'checkboxes': {"function": "params.data.year < 2007"},
},
# other grid options...
}
Note that:
rowSelection.hideDisabledCheckboxes to True to hide disabled checkboxes.```python
import dash_ag_grid as dag
from dash import Dash, html, dcc, Input, Output, callback
import pandas as pd
import json
app = Dash()
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/ag-grid/olympic-winners.csv"
)
columnDefs = [{"field": i, 'editable': i == "year"} for i in ["country", "year", "athlete", "age", "sport", "total"]]
app.layout = html.Div(
[
dcc.Checklist(
id='chk-single-row-selection-remove-checkboxes-function',
options={
'function': 'Disable checkboxes using a function',
'hide': 'Hide disabled checkboxes',
},
value=[]
),
dag.AgGrid(
id="grid-single-row-selection-remove-checkboxes-function",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="sizeToFit",
dashGridOptions={"rowSelection": {'mode': 'singleRow'}}
),
html.Pre(id="pre-single-row-selection-remove-checkboxes-function")
],
)
@callback(
Output("grid-single-row-selection-remove-checkboxes-function", "dashGridOptions"),
Output("pre-single-row-selection-remove-checkboxes-function", "children"),
Input("chk-single-row-selection-remove-checkboxes-function", "value"),
)
def checkboxes_options(selected_options):
grid_options: dict = {"rowSelection": {'mode': 'singleRow'}}
if 'function' in selected_options:
grid_options["rowSelection"]['checkboxes'] = {"function": "params.data.year < 2007"}
if 'hide' in selected_options:
grid_options["rowSelection"]['hideDisabledCheckboxes'] = True
return grid_options, f'dashGridOptions = {json.dumps(grid_options, indent=4).replace("true", "True")}'
if __name__ == "__main__":
app.run(debug=True)
```
It is possible to specify which rows can be selected via the rowSelection.isRowSelectable callback function.
For instance, if we only wanted to allow selection for rows where the ‘year’ property is less than 2007, we could
implement the following:
dashGridOptions = {
'rowSelection': {
'mode': 'singleRow',
'isRowSelectable': {"function": "params.data.year < 2007"},
},
# other grid options...
}
Rows for which isRowSelectable returns False cannot be selected at all, whether using the UI or the API.
Note that:
rowSelection.hideDisabledCheckboxes to True to hide disabled checkboxes.```python
import dash_ag_grid as dag
from dash import Dash, html, dcc, Input, Output, callback, Patch
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, 'editable': i == "year"} for i in ["country", "year", "athlete", "age", "sport", "total"]]
app.layout = html.Div(
[
dcc.Checklist(
id='chk-single-row-selection-selectable-function',
options={'hide': 'Hide disabled checkboxes'},
value=[]
),
dag.AgGrid(
id="grid-single-row-selection-selectable-function",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="sizeToFit",
dashGridOptions={
"rowSelection": {
'mode': 'singleRow',
'isRowSelectable': {"function": "params.data.year < 2007"},
}
}
)
],
)
@callback(
Output("grid-single-row-selection-selectable-function", "dashGridOptions"),
Input("chk-single-row-selection-selectable-function", "value"),
)
def hide_checkboxes(selected_options):
grid_options_patch = Patch()
grid_options_patch["rowSelection"]['hideDisabledCheckboxes'] = 'hide' in selected_options
return grid_options_patch
if __name__ == "__main__":
app.run(debug=True)
```
The checkbox column may be customized in a similar way to any other column, by specifying its column definition in the
selectionColumnDef grid option.
selectionColumnDef:”>https://www.ag-grid.com/archive/32.3.2/react-data-grid/row-selection-single-row/#reference-selection-selectionColumnDef):columnDefs, which does not support several normal column features such asThe SelectionColumnDef allows for a great deal of customization, including custom renderers, sorting, tooltips and
more. The example below demonstrates allowing sorting using the default sort order (selected first), changing the
default width of the column, making the column resizable and adding some header tooltip text.
The checkbox column has a default
maxWidthset. To make the columnresizable(when settingresizable: True), or
to set awidth/initialWidth, the max width must also be overridden.
<br>
dashGridOptions = {
'selectionColumnDef': {
'sortable': True,
'width': 100,
'maxWidth': 100,
'resizable': True,
'headerTooltip': 'Checkboxes indicate selection',
},
# other grid options...
}
```python
import dash_ag_grid as dag
from dash import Dash
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 = dag.AgGrid(
id="grid-single-row-selection-custom-checkbox-col",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="sizeToFit",
dashGridOptions={
"rowSelection": {'mode': 'singleRow'},
'selectionColumnDef': {
'sortable': True,
'width': 100,
'maxWidth': 100,
'resizable': True,
'headerTooltip': 'Checkboxes indicate selection',
},
}
)
if __name__ == "__main__":
app.run(debug=True)
```
See AG Grid docs API Reference for the full
list of configuration options available in 'singleRow' mode.
<br>