DevExtreme React - Selection
User Interaction
The DataGrid widget supports single and multiple row selection. Use the selection.mode option to change the current mode.
In the single mode, only one row can be selected at a time, while in the multiple mode, several rows can be selected with check boxes that appear in the selection column.
The check box in the column's header selects all rows or only the currently rendered ones, depending on the selectAllMode. Note that clicking this check box selects/deselects only those rows that meet filtering conditions if a filter is applied.
You can prevent users from selecting all rows by setting the selection.allowSelectAll option to false.
The showCheckBoxesMode option specifies when the widget renders check boxes in the selection column. For example, the following code tells the widget to never render them, though a user can still select rows using keyboard shortcuts:
Single Selection Demo Multiple Selection Demo
See Also
Initial and Runtime Selection
Use the selectedRowKeys option to select rows initially. Note that to access a row by its key, you should specify the DataGrid's keyExpr or the Store's key option.
The DataGrid provides two methods that select rows at runtime: selectRows(keys, preserve) and selectRowsByIndexes(indexes). They both clear the previous selection by default, although with the selectRows(keys, preserve) method you can keep it if you pass true as the preserve parameter. Before selecting a row, you can call the isRowSelected(key) method to check if this row is not already selected.
To select all rows at once, call the selectAll() method.
Call the getSelectedRowKeys() or getSelectedRowsData() method to get the selected row's keys or data.
See Also
Clear Selection Settings
Call the deselectRows(keys) method to clear the selection of specific rows.
Call the clearSelection() method to clear selection of all rows. If you apply a filter and want to keep the selection of invisible rows that do not meet the filtering conditions, use the deselectAll() method. Also call this method to clear selection depending on the selectAllMode.
See Also
- Call Methods: jQuery | Angular | AngularJS | Knockout | ASP.NET MVC
Events
The DataGrid widget raises the selectionChanged event when a row is selected, or the selection is cleared. If the function that handles this event is going to remain unchanged, assign it to the onSelectionChanged option when you configure the widget. Note that information on selected and deselected rows is passed to the handler only when selection is not deferred.
If you are going to change the event handler at runtime, or if you need to attach several handlers to the event, subscribe to it using the on(eventName, eventHandler) method. This approach is more typical of jQuery.
- var selectionChangedEventHandler1 = function(e) {
- // First handler of the "selectionChanged" event
- };
- var selectionChangedEventHandler2 = function(e) {
- // Second handler of the "selectionChanged" event
- };
- $("#dataGridContainer").dxDataGrid("instance")
- .on("selectionChanged", selectionChangedEventHandler1)
- .on("selectionChanged", selectionChangedEventHandler2);
See Also
- Handle Events: jQuery | Angular | AngularJS | Knockout | ASP.NET MVC
If you have technical questions, please create a support ticket in the DevExpress Support Center.