| layout | post |
|---|---|
| title | Row selection in React Grid component | Syncfusion |
| description | Learn here all about Row selection in Syncfusion React Grid component of Syncfusion Essential JS 2 and more. |
| control | Row selection |
| platform | ej2-react |
| documentation | ug |
| domainurl |
Row selection in the Grid component allows you to interactively select specific rows or ranges of rows within the grid. This selection can be done effortlessly through mouse clicks or arrow keys (up, down, left, and right). This feature is useful when you want to highlight, manipulate, or perform actions on specific row within the Grid.
To enable row selection, you should set the selectionSettings.mode property to either Row or Both. This property determines the selection mode of the grid.
Single row selection allows you to select a single row at a time within the Grid. This feature is useful when you want to focus on specific rows or perform actions on the data within a particular row.
To enable single row selection, set the selectionSettings.mode property to Row and the selectionSettings.type property to Single. This configuration allows you to select a only one row at a time within the grid.
Here's an example of how to enable single row selection using properties:
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% include code-snippet/grid/selection-row-cs1/app/App.jsx %} {% endhighlight %} {% highlight ts tabtitle="App.tsx" %} {% include code-snippet/grid/selection-row-cs1/app/App.tsx %} {% endhighlight %} {% highlight js tabtitle="datasource.jsx" %} {% include code-snippet/grid/selection-row-cs1/app/datasource.jsx %} {% endhighlight %} {% highlight ts tabtitle="datasource.tsx" %} {% include code-snippet/grid/selection-row-cs1/app/datasource.tsx %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/code-snippet/grid/selection-row-cs1" %}
Multiple row selection allows you to select multiple rows within the Grid. This feature is valuable when you need to perform actions on several rows simultaneously or focus on specific data areas.
To enable multiple row selection, set the selectionSettings.mode property to Row and the selectionSettings.type property to Multiple. This configuration allows you to select a multiple rows at a time within the grid.
Here's an example of how to enable multiple rows selection using properties:
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% include code-snippet/grid/selection-row-cs2/app/App.jsx %} {% endhighlight %} {% highlight ts tabtitle="App.tsx" %} {% include code-snippet/grid/selection-row-cs2/app/App.tsx %} {% endhighlight %} {% highlight js tabtitle="datasource.jsx" %} {% include code-snippet/grid/selection-row-cs2/app/datasource.jsx %} {% endhighlight %} {% highlight ts tabtitle="datasource.tsx" %} {% include code-snippet/grid/selection-row-cs2/app/datasource.tsx %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/code-snippet/grid/selection-row-cs2" %}
You have the ability to select a specific row during the initial rendering of the Grid component. This feature is particularly useful when you want to highlight or pre-select a specific row in the grid. To achieve this, you can utilize the selectedRowIndex property provided by the Grid component.
In the following example, it demonstrates how to select a row at initial rendering:
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% include code-snippet/grid/selection-cs7/app/App.jsx %} {% endhighlight %} {% highlight ts tabtitle="App.tsx" %} {% include code-snippet/grid/selection-cs7/app/App.tsx %} {% endhighlight %} {% highlight js tabtitle="datasource.jsx" %} {% include code-snippet/grid/selection-cs7/app/datasource.jsx %} {% endhighlight %} {% highlight ts tabtitle="datasource.tsx" %} {% include code-snippet/grid/selection-cs7/app/datasource.tsx %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/code-snippet/grid/selection-cs7" %}
The Grid allows you to select rows in any page based on their index value. This feature is useful when you want to perform specific actions on rows, such as highlighting, applying styles, or executing operations, regardless of their location across multiple pages within the grid.
To achieve this, you can utilize the selectRow method and the goToPage method of the Grid control. By handling the change event of DropDownList component, you can implement the logic to navigate to the desired page and select the row based on the index value.
Additionally, by handling the actionComplete event of the Grid, you can maintain the selection of the desired row after completing the paging action.
The following example demonstrates how to select rows in any page based on index value using actionComplete and change event:
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% raw %} import { ChangeEventArgs, DropDownListComponent } from '@syncfusion/ej2-react-dropdowns' import { ColumnDirective, ColumnsDirective, GridComponent, Page, PageSettingsModel, Inject, PageEventArgs } from '@syncfusion/ej2-react-grids'; import * as React from 'react'; import { data } from './datasource';
function App() { let grid; const pageSettings = { pageSize: 10 } let mod; let value; const ddlData = [ { text: 'Select row index' }, { text: '1', value: '1' }, { text: '2', value: '2' }, { text: '30', value: '30' }, { text: '80', value: '80' }, { text: '110', value: '110' }, { text: '120', value: '120' }, { text: '210', value: '210' }, { text: '310', value: '310' }, { text: '410', value: '410' }, { text: '230', value: '230' } ] const actionComplete = (args) => { if (args.requestType === "paging") { grid.selectRow(mod); } } const valueChange = (args) => { value = +args.value; mod = (value - 1) % 10; const page = Math.ceil(value / 10);
if (page === 1) {
if (grid.pagerModule.pagerObj.currentPage !== 1) {
grid.pagerModule.goToPage(1);
}
grid.selectRow(mod);
}
else {
grid.pagerModule.goToPage(page);
if (grid.pagerModule.pagerObj.currentPage === page) {
grid.selectRow(mod);
}
}
}
return (
function App() { let grid: GridComponent | null; const pageSettings: PageSettingsModel = { pageSize: 10 } let mod; let value; const ddlData: { [key: string]: Object; }[] = [ { text: 'Select row index' }, { text: '1', value: '1' }, { text: '2', value: '2' }, { text: '30', value: '30' }, { text: '80', value: '80' }, { text: '110', value: '110' }, { text: '120', value: '120' }, { text: '210', value: '210' }, { text: '310', value: '310' }, { text: '410', value: '410' }, { text: '230', value: '230' } ] const actionComplete = (args: PageEventArgs) => { if (args.requestType === "paging") { (grid as GridComponent).selectRow(mod); } } const valueChange = (args: ChangeEventArgs) => { value = +args.value; mod = (value - 1) % 10; const page = Math.ceil(value / 10);
if (page === 1) {
if ((grid as GridComponent).pagerModule.pagerObj.currentPage !== 1) {
(grid as GridComponent).pagerModule.goToPage(1);
}
(grid as GridComponent).selectRow(mod);
}
else {
(grid as GridComponent).pagerModule.goToPage(page);
if ((grid as GridComponent).pagerModule.pagerObj.currentPage === page) {
(grid as GridComponent).selectRow(mod);
}
}
}
return (
{% previewsample "page.domainurl/code-snippet/grid/selection-row-cs3" %}
The Grid component allows you to perform multiple row selection by simply clicking on rows one by one without pressing CTRL or SHIFT keys. This means that when you click on a row, it will be selected, and clicking on another row will add it to the selection without deselecting the previously selected rows. To deselect a previously selected row, you can click on the row again, and it will be unselected.
To enable the simple multiple row selection, you need to set the selectionSettings.enableSimpleMultiRowSelection property to true.
The following example demonstrates how to enable multiple row selection with a single click on the Grid row using enableSimpleMultiRowSelection property:
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% raw %} import { SwitchComponent } from '@syncfusion/ej2-react-buttons' import { ColumnDirective, ColumnsDirective, GridComponent, SelectionSettingsModel } from '@syncfusion/ej2-react-grids'; import * as React from 'react'; import { data } from './datasource';
function App() { let grid const selectionSettings = { type: 'Multiple' }; const valueChange = (args) => { grid.selectionSettings.enableSimpleMultiRowSelection = args.checked; }
return (
function App() { let grid: GridComponent | null const selectionSettings: SelectionSettingsModel = { type: 'Multiple' }; const valueChange = (args: ChangeEventArgs) => { (grid as GridComponent).selectionSettings.enableSimpleMultiRowSelection = args.checked; }
return (
{% previewsample "page.domainurl/code-snippet/grid/selection-cs9" %}
You can perform single row selection, multiple row selection, and range of row selection externally in a Grid using built-in methods. This feature allows you to interact with specific rows within the Grid. The following topic demonstrates how you can achieve these selections using methods.
Single row selection allows you to select a single row at a time within the Grid. This feature is useful when you want to focus on specific rows or perform actions on the data within a particular row.
To achieve single row selection, you can use the selectRow method. This method allows you to programmatically select a specific row within the Grid by specifying its index.
The following example demonstrates how to select a single row within the Grid by obtaining the selected row index through a textbox component and passing these row index as argument to the selectRow method. When the button event is triggered by clicking the Select Row button, a single row is selected within the Grid:
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% raw %} import { TextBoxComponent } from '@syncfusion/ej2-react-inputs' import { ButtonComponent } from '@syncfusion/ej2-react-buttons' import { ColumnDirective, ColumnsDirective, GridComponent, SelectionSettingsModel } from '@syncfusion/ej2-react-grids'; import * as React from 'react'; import { data } from './datasource';
function App() { let grid; let textBoxRow; let rowIndex; const selectionSettings = { mode: 'Row', type: 'Single' }; const click = () => { rowIndex = parseInt(textBoxRow.element.value, 10); if (!isNaN(rowIndex)) { grid.selectRow(rowIndex); } } return (
function App() { let grid: GridComponent | null; let textBoxRow: TextBoxComponent | null; let rowIndex: number; const selectionSettings: SelectionSettingsModel = { mode: 'Row', type: 'Single' }; const click = () => { rowIndex = parseInt((textBoxRow as TextBoxComponent).element.value, 10); if (!isNaN(rowIndex)) { (grid as GridComponent).selectRow(rowIndex); } } return (
{% previewsample "page.domainurl/code-snippet/grid/selection-row-cs5" %}
The React Grid allows you to select multiple rows within the grid simultaneously. This feature is valuable when you need to perform actions or operations on several rows at once or focus on specific areas of your data.
To achieve multiple row selection, you can use the selectRows method. This method allows you to select a collection of rows by specifying their indexes, giving you the ability to interact with multiple rows together.
The following example, demonstrates how to select multiple rows in the Grid by calling the selectRows method within the button click event and passing an array of row indexes as arguments.
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% raw %} import { ButtonComponent } from '@syncfusion/ej2-react-buttons' import { ColumnDirective, ColumnsDirective, GridComponent, SelectionSettingsModel } from '@syncfusion/ej2-react-grids'; import * as React from 'react'; import { data } from './datasource';
function App() { let grid; const selectionSettings = { mode: 'Row', type: 'Multiple' }; const buttonClick = (rowIndexes) => { grid.clearRowSelection(); grid.selectRows(rowIndexes); } return (
function App() { let grid: GridComponent | null; const selectionSettings: SelectionSettingsModel = { mode: 'Row', type: 'Multiple' }; const buttonClick = (rowIndexes: number[]) => { (grid as GridComponent).clearRowSelection(); (grid as GridComponent).selectRows(rowIndexes); } return (
{% previewsample "page.domainurl/code-snippet/grid/selection-row-cs4" %}
Range of row selection in the Grid enables you to select a continuous range of rows within the grid. This feature is particularly useful when you want to perform actions on multiple rows simultaneously or focus on a specific range of data.
To achieve range of row selection, you can use the selectRowsByRange method. This method selects a range of rows from start and end row indexes.
The following example, demonstrates how to select a range of rows within the Grid by obtaining the selected rows start index and end index through textbox components. Then, pass these start index and end index as arguments to the selectRowsByRange method. When you trigger the button event by clicking the Select Rows button, a range of rows is selected within the Grid.
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% raw %} import { TextBoxComponent } from '@syncfusion/ej2-react-inputs' import { ButtonComponent } from '@syncfusion/ej2-react-buttons' import { ColumnDirective, ColumnsDirective, GridComponent, SelectionSettingsModel } from '@syncfusion/ej2-react-grids'; import * as React from 'react'; import { data } from './datasource';
function App() { let grid; let textBox1; let textBox2; let startRowIndex; let endRowIndex; const selectionSettings = { mode: 'Row', type: 'Multiple' }; const click = () => { startRowIndex = parseInt(textBox1.value, 10); endRowIndex = parseInt(textBox2.value, 10); grid.selectionModule.clearRowSelection(); if (!isNaN(startRowIndex) && !isNaN(endRowIndex)) { grid.selectRowsByRange(startRowIndex, endRowIndex); } } return (
function App() { let grid: GridComponent | null; let textBox1: TextBoxComponent | null; let textBox2: TextBoxComponent | null; let startRowIndex: number; let endRowIndex: number; const selectionSettings: SelectionSettingsModel = { mode: 'Row', type: 'Multiple' }; const click = () => { startRowIndex = parseInt((textBox1 as TextBoxComponent).value, 10); endRowIndex = parseInt((textBox2 as TextBoxComponent).value, 10); (grid as GridComponent).selectionModule.clearRowSelection(); if (!isNaN(startRowIndex) && !isNaN(endRowIndex)) { (grid as GridComponent).selectRowsByRange(startRowIndex, endRowIndex); } } return (
{% previewsample "page.domainurl/code-snippet/grid/selection-row-cs6" %}
You can programmatically select specific rows in the React Grid component based on a certain condition. This feature is particularly useful when you need to dynamically highlight or manipulate specific rows in the grid based on custom conditions. This functionality can be achieved using the selectRows method in the dataBound event of Grid.
In the below demo, we have selected the grid rows only when EmployeeID column value greater than 3.
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% include code-snippet/grid/change-headertext-cs2/app/App.jsx %} {% endhighlight %} {% highlight ts tabtitle="App.tsx" %} {% include code-snippet/grid/change-headertext-cs2/app/App.tsx %} {% endhighlight %} {% highlight js tabtitle="datasource.jsx" %} {% include code-snippet/grid/change-headertext-cs2/app/datasource.jsx %} {% endhighlight %} {% highlight ts tabtitle="datasource.tsx" %} {% include code-snippet/grid/change-headertext-cs2/app/datasource.tsx %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/code-snippet/grid/change-headertext-cs2" %}
You can retrieve the indexes of the currently selected rows in the Grid component. This feature is particularly useful when you need to perform actions or operations specifically on the selected rows.
To achieve this, you can leverage the getSelectedRowIndexes method, which returns an array of numbers representing the indexes of the selected rows.
The following example demonstrates how to get selected row indexes using getSelectedRowIndexes method:
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% include code-snippet/grid/selection-cs8/app/App.jsx %} {% endhighlight %} {% highlight ts tabtitle="App.tsx" %} {% include code-snippet/grid/selection-cs8/app/App.tsx %} {% endhighlight %} {% highlight js tabtitle="datasource.jsx" %} {% include code-snippet/grid/selection-cs8/app/datasource.jsx %} {% endhighlight %} {% highlight ts tabtitle="datasource.tsx" %} {% include code-snippet/grid/selection-cs8/app/datasource.tsx %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/code-snippet/grid/selection-cs8" %}
The Grid component allows you to retrieve the selected records even when navigating to different pages. This feature is useful when working with large data sets and allows you to perform actions on the selected records across multiple pages.
To persist the selection across pages, you need to enable the persist selection property. By default, this property is set to false. To enable it, set the value to true in the selectionSettings property of the Grid component.
To retrieve the selected records from different pages, you can use the getSelectedRecords method. This method returns an array of the selected records.
The following example demonstrates how to retrieve selected records from various pages using the getSelectedRecords method and display OrderID in a dialog when a button is clicked:
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% include code-snippet/grid/selection-cs12/app/App.jsx %} {% endhighlight %} {% highlight ts tabtitle="App.tsx" %} {% include code-snippet/grid/selection-cs12/app/App.tsx %} {% endhighlight %} {% highlight js tabtitle="datasource.jsx" %} {% include code-snippet/grid/selection-cs12/app/datasource.jsx %} {% endhighlight %} {% highlight ts tabtitle="datasource.tsx" %} {% include code-snippet/grid/selection-cs12/app/datasource.tsx %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/code-snippet/grid/selection-cs12" %}
To persist the grid selection, it is necessary to define any one of the columns as a primary key using the columns property.
The get selected records allows you to retrieve the data of the selected rows from the Grid component. This can be particularly useful when you need to perform actions on the selected data or display specific information based on the selected rows.
To retrieve the selected records, you can use the getSelectedRecords method. This method allows you to obtain an array of objects representing the selected records.
Here's an example that displays the selected row count using the getSelectedRecords method:
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% include code-snippet/grid/selection-row-cs7/app/App.jsx %} {% endhighlight %} {% highlight ts tabtitle="App.tsx" %} {% include code-snippet/grid/selection-row-cs7/app/App.tsx %} {% endhighlight %} {% highlight js tabtitle="datasource.jsx" %} {% include code-snippet/grid/selection-row-cs7/app/datasource.jsx %} {% endhighlight %} {% highlight ts tabtitle="datasource.tsx" %} {% include code-snippet/grid/selection-row-cs7/app/datasource.tsx %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/code-snippet/grid/selection-row-cs7" %}
Clearing row selection programmatically in the Grid component is a useful feature when you want to remove any existing row selections. To achieve this, you can use the clearRowSelection method.
The
clearRowSelectionmethod is applicable when the selection type is set to Multiple or Single.
The following example demonstrates how to clear row selection by calling the clearRowSelection method in the button click event.
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% include code-snippet/grid/selection-cs11/app/App.jsx %} {% endhighlight %} {% highlight ts tabtitle="App.tsx" %} {% include code-snippet/grid/selection-cs11/app/App.tsx %} {% endhighlight %} {% highlight js tabtitle="datasource.jsx" %} {% include code-snippet/grid/selection-cs11/app/datasource.jsx %} {% endhighlight %} {% highlight ts tabtitle="datasource.tsx" %} {% include code-snippet/grid/selection-cs11/app/datasource.tsx %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/code-snippet/grid/selection-cs11" %}
The Grid provides several events related to row selection that allow you to respond to and customize the behavior of row selection. These events give you control over various aspects of row selection. Here are the available row selection events:
rowSelecting: This event is triggered before any row selection occurs. It provides an opportunity to implement custom logic or validation before a row is selected, allowing you to control the selection process.
rowSelected: This event is triggered after a row is successfully selected. You can use this event to perform actions or updates when a row is selected.
rowDeselecting: This event is triggered just before a selected row is deselected. It allows you to perform custom logic or validation to decide whether the row should be deselected or not.
rowDeselected: This event is triggered when a particular selected row is deselected. You can use this event to perform actions or validations when a row is no longer selected.
In the following example, row selection is canceled when the value of CustomerID is equal to VINET within the rowSelecting event. The background color changes to green when the value of Freight is greater than 10 and less than or equal to 140, triggering the rowDeselected event. The background color changes to red when the value of Freight is less than or equal to 10 during the rowDeselected event. Furthermore, the background color changes to yellow when the value of Freight is greater than 140 during the rowDeselected event. A notification message is displayed to indicate which event was triggered whenever a row is selected.
{% tabs %} {% highlight js tabtitle="App.jsx" %} {% include code-snippet/grid/selection-row-cs8/app/App.jsx %} {% endhighlight %} {% highlight ts tabtitle="App.tsx" %} {% include code-snippet/grid/selection-row-cs8/app/App.tsx %} {% endhighlight %} {% highlight js tabtitle="datasource.jsx" %} {% include code-snippet/grid/selection-row-cs8/app/datasource.jsx %} {% endhighlight %} {% highlight ts tabtitle="datasource.tsx" %} {% include code-snippet/grid/selection-row-cs8/app/datasource.tsx %} {% endhighlight %} {% endtabs %}
{% previewsample "page.domainurl/code-snippet/grid/selection-row-cs8" %}