All docs
V24.2
24.2
24.1
23.2
23.1
22.2
22.1
21.2
21.1
20.2
20.1
19.2
The page you are viewing does not exist in version 19.2.
19.1
The page you are viewing does not exist in version 19.1.
18.2
The page you are viewing does not exist in version 18.2.
18.1
The page you are viewing does not exist in version 18.1.
17.2
The page you are viewing does not exist in version 17.2.

JavaScript/jQuery DataGrid - Selection

NOTE
You must specify the DataGrid's keyExpr or the Store's key property to ensure that selection works properly.
See Also

User Interaction

The DataGrid UI component supports single and multiple row selection. Use the selection.mode property to change the current mode.

JavaScript
  • $(function() {
  • $("#dataGridContainer").dxDataGrid({
  • // ...
  • selection: {
  • mode: "single" // or "multiple" | "none"
  • }
  • });
  • });

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.

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component DataGrid Sorting

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.

JavaScript
  • $(function() {
  • $("#dataGridContainer").dxDataGrid({
  • // ...
  • selection: {
  • mode: "multiple",
  • selectAllMode: "page" // or "allPages"
  • }
  • });
  • });

You can prevent users from selecting all rows by setting the selection.allowSelectAll property to false.

JavaScript
  • $(function() {
  • $("#dataGridContainer").dxDataGrid({
  • // ...
  • selection: {
  • mode: "multiple",
  • allowSelectAll: false
  • }
  • });
  • });

The showCheckBoxesMode the property specifies when the UI component renders checkboxes in the selection column. For example, the following code tells the UI component to never render them, though a user can still select rows using keyboard shortcuts:

JavaScript
  • $(function() {
  • $("#dataGridContainer").dxDataGrid({
  • // ...
  • selection: {
  • mode: "multiple",
  • showCheckBoxesMode: "none" // or "onClick" | "onLongTap" | "always"
  • }
  • });
  • });

Single Selection Demo Multiple Selection Demo

See Also

API

Initial and Runtime Selection

Use the selectedRowKeys property 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 property.

JavaScript
  • $(function() {
  • $("#dataGridContainer").dxDataGrid({
  • // ...
  • dataSource: {
  • store: {
  • // ...
  • key: "id"
  • }
  • },
  • selectedRowKeys: [1, 5, 18]
  • });
  • });

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.

JavaScript
  • var selectSingleRow = function (dataGridInstance, key, preserve) {
  • if (!dataGridInstance.isRowSelected(key)) {
  • dataGridInstance.selectRows([key], preserve);
  • }
  • }
JavaScript
  • $("#dataGridContainer").dxDataGrid({
  • // ...
  • onContentReady: function (e) {
  • // Selects the first visible row
  • e.component.selectRowsByIndexes([0]);
  • }
  • }).dxDataGrid("instance");

To select all rows at once, call the selectAll() method.

JavaScript
  • dataGrid.selectAll();

View Demo

Call the getSelectedRowKeys() or getSelectedRowsData() method to get the selected row's keys or data.

JavaScript
  • var dataGrid = $("#dataGridContainer").dxDataGrid("instance");
  • var selectedKeys = dataGrid.getSelectedRowKeys();
  • var selectedData = dataGrid.getSelectedRowsData();
See Also

Clear Selection Settings

Call the deselectRows(keys) method to clear the selection of specific rows.

JavaScript
  • $("#dataGridContainer").dxDataGrid("deselectRows", [1, 4, 10]);

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.

JavaScript
  • var dataGrid = $("#dataGridContainer").dxDataGrid("instance");
  • dataGrid.deselectAll();
  • dataGrid.clearSelection();

View Demo

See Also

Events

The DataGrid UI component 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 property when you configure the UI component. Note that information on selected and deselected rows is passed to the handler only when selection is not deferred.

JavaScript
  • $(function() {
  • $("#dataGridContainer").dxDataGrid({
  • onSelectionChanged: function(e) { // Handler of the "selectionChanged" event
  • var currentSelectedRowKeys = e.currentSelectedRowKeys;
  • var currentDeselectedRowKeys = e.currentDeselectedRowKeys;
  • var allSelectedRowKeys = e.selectedRowKeys;
  • var allSelectedRowsData = e.selectedRowsData;
  • // ...
  • }
  • });
  • });

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.

JavaScript
  • 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