To enable rows selection, set the Grid Option rowSelection
to either 'single'
or 'multiple'
:
'single'
will use single row selection, when you select a row, any previously selected row gets unselected.'multiple'
allows multiple rows to be selected. To select multiple rows, it is possible to:Moreover, it is also possible to use the following Grid Options to fine-tune the row selection behavior:
rowMultiSelectWithClick
: Set to True
to allow multiple rows to be selected with clicks only. For example, if yousuppressRowDeselection
: By default, the grid allows deselection of rows holding <kbd>Ctrl<kbd> while clicking onTrue
to prevent rows from being deselected if you hold down <kbd>Ctrl<kbd> and click the row. OncesuppressRowClickSelection
: If True
, rows won’t be selected when clicked. Use, for example, when you want checkboxThe following example shows the Grid behavior using the different Grid Options to select the rows.
Using rowSelection='single'
:
suppressRowDeselection
is enabled.Using rowSelection='multiple'
:
rowMultiSelectWithClick: True
enables multiple row selection with clicks without needing to press <kbd>suppressRowDeselection
is enabled.It is possible to specify which rows can be selected via the isRowSelectable
function.
isRowSelectable
- Function to be used to determine which rows are selectable. By default, rows are selectable, sofalse
to make a row un-selectable.For instance if we only wanted to allow rows where the ‘year’ field is more than 2007, we could implement the
following:
dashGridOptions = {
'rowSelection': "multiple",
'isRowSelectable': {"function": "params.data.year > 2007"}
}
Note that it can be a good idea to use checkbox selection to help to identify the selectable rows as the checkbox of the
non-selectable rows will be hidden, see
example Selectable Rows with Checkboxes.
The following example shows the result setting the above parameters, only rows having year > 2007 are selectable. The
multi-rows selection is enabled, try to select a range of rows using <kbd>Shift<kbd>, only the selectable rows will be
selected.
selectedRows
ParameterThe Grid parameter selectedRows
can be used to:
The selectedRows
parameter can be set 3 ways:
df.head(5).to_dict("records")
{"ids": ["1", "3", "5"]}
{"function": "params.data.total > 5"}
Note that selecting hundreds of rows using rowData of the rows to select can show lags, setting getRowId
can improve
the performance. Using row ids or a function to select hundreds of rows generally provides even better performance.
See Improve Selection Performance.
In the following example selectedRows
is used:
This second example shows how to generate a popup using Dash Bootstrap Components when a user selects a row in the
grid.
This example shows the selection performance selecting hundreds of rows when setting selectedRows
with the following
options:
getRowId
The button can be used to select/unselect the rows, and we can see the time to perform the selection.
Note that the function used in this example {'function': 'params.data.age == 23'}
selects 691 rows. To compare the
results, the other options are also set to select 691 rows.
The first option can take a few seconds to perform the selection. The second greatly improves the performance.
The third and the final options provide even better performance.
Dash AG Grid provides the deleteSelectedRows
parameter that can be used to easily remove the selected rows. When set to
True
under the hood it will trigger
a transaction gridApi.applyTransaction({remove: selection})
In the following example, try to select a few rows and then click on the button to remove them.