Search results

Selection

Selection provides an option to highlight a row or cell. Selection can be done through simple Mouse down or Arrow keys. To disable selection in the Grid, set the allowSelection to false.

The grid supports two types of selection that can be set by using the selectionSettings.type. They are:

  • Single - The Single value is set by default. Allows you to select only a single row or cell.
  • Multiple - Allows you to select multiple rows or cells. To perform the multi-selection, press and hold CTRL key and click the desired rows or cells. To select range of rows or cells, press and hold the SHIFT key and click the rows or cells.
Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, ColumnsDirective, ColumnDirective, SelectionSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from '../datasource.ts';

class App extends React.Component<{}, {}>{
    public data: Object[] = data;
    public settings: SelectionSettingsModel = { type: 'Multiple' };
    render() {
        return <GridComponent dataSource={this.data} selectionSettings={this.settings} height={315}>
                <ColumnsDirective>
                 <ColumnDirective field='OrderID' width='120' textAlign="Right"></ColumnDirective>
                 <ColumnDirective field='CustomerID' width='150'></ColumnDirective>
                 <ColumnDirective field='ShipCity' width='100'></ColumnDirective>
                 <ColumnDirective field='ShipName' width='150'></ColumnDirective>
                </ColumnsDirective>
               </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

Selection Mode

The grid supports three types of selection mode that can be set by using the selectionSettings.mode. They are:

  • Row - The Row value is set by default. Allows you to select rows only.
  • Cell - Allows you to select cells only.
  • Both - Allows you to select rows and cells at the same time.
Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, ColumnsDirective, ColumnDirective, SelectionSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from '../datasource.ts';

class App extends React.Component<{}, {}>{
    public data: Object[] = data;
    public settings: SelectionSettingsModel = { mode: 'Both' };
    render() {
        return <GridComponent dataSource={this.data} selectionSettings={this.settings} height={315}>
                <ColumnsDirective>
                 <ColumnDirective field='OrderID' width='120' textAlign="Right"></ColumnDirective>
                 <ColumnDirective field='CustomerID' width='150'></ColumnDirective>
                 <ColumnDirective field='ShipCity' width='100'></ColumnDirective>
                 <ColumnDirective field='ShipName' width='150'></ColumnDirective>
                </ColumnsDirective>
               </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

Cell Selection

Cell Selection can be done through simple Mouse down or Arrow keys(up, down, left and right).

The grid supports two types of cell selection mode that can be set by using the selectionSettings.cellSelectionMode. They are:

  • Flow - The Flow value is set by default. Select range of cells between the start index and end index which includes in between cells of rows.
  • Box - Select range of cells within the start and end column indexes which includes in between cells of rows within the range.
Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, ColumnsDirective, ColumnDirective, SelectionSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from '../datasource.ts';

class App extends React.Component<{}, {}>{
    public data: Object[] = data;
    public settings: SelectionSettingsModel = { cellSelectionMode: 'Box', type: 'Multiple', mode: 'Cell' };
    render() {
        return <GridComponent dataSource={this.data} selectionSettings={this.settings} height={315}>
                <ColumnsDirective>
                 <ColumnDirective field='OrderID' width='120' textAlign="Right"></ColumnDirective>
                 <ColumnDirective field='CustomerID' width='150'></ColumnDirective>
                 <ColumnDirective field='ShipCity' width='100'></ColumnDirective>
                 <ColumnDirective field='ShipName' width='150'></ColumnDirective>
                </ColumnsDirective>
               </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

Cell Selection requires the selectionSettings.mode to be Cell or Both and type should be Multiple.

Checkbox selection

Checkbox selection provides an option to select multiple grid records with help of checkbox in each row.

To render the checkbox in each grid row, you need to use checkbox column with type as checkbox using the column type property.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, ColumnsDirective, ColumnDirective } from '@syncfusion/ej2-react-grids';
import { data } from '../datasource.ts';

class App extends React.Component<{}, {}>{
    public data: Object[] = data;
    render() {
        return <GridComponent dataSource={this.data} height={315}>
                <ColumnsDirective>
                 <ColumnDirective type='checkbox' width='50'></ColumnDirective>
                 <ColumnDirective field='OrderID' width='120' textAlign="Right"></ColumnDirective>
                 <ColumnDirective field='CustomerID' width='150'></ColumnDirective>
                 <ColumnDirective field='ShipCity' width='100'></ColumnDirective>
                 <ColumnDirective field='ShipName' width='150'></ColumnDirective>
                </ColumnsDirective>
               </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

By default, selection is allowed by clicking a grid row or checkbox in that row. To allow selection only through checkbox, you can set the selectionSettings.checkboxOnly property to true. Selection can be persisted in all the operations using the selectionSettings.persistSelection property. For persisting selection on the grid, any one of the columns should be defined as a primary key using the columns.isPrimaryKey property.

Checkbox selection Mode

In checkbox selection, selection can also be done by clicking on rows. This selection provides two types of Checkbox Selection mode which can be set by using the following API, selectionSettings.checkboxMode. The modes are;

  • Default: This is the default value of the checkboxMode. In this mode, user can select multiple rows by clicking rows one by one.
  • ResetOnRowClick: In ResetOnRowClick mode, when user clicks on a row it will reset previously selected row. Also you can perform multiple-selection in this mode by press and hold CTRL key and click the desired rows. To select range of rows, press and hold the SHIFT key and click the rows.
Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, ColumnsDirective, ColumnDirective, SelectionSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from '../datasource.ts';

class App extends React.Component<{}, {}>{
    public data: Object[] = data;
    public settings: SelectionSettingsModel = { checkboxMode: 'ResetOnRowClick'};
    render() {
        return <GridComponent dataSource={this.data} height={315} selectionSettings={this.settings}>
                <ColumnsDirective>
                 <ColumnDirective type='checkbox' width='50'></ColumnDirective>
                 <ColumnDirective field='OrderID' width='120' textAlign="Right"></ColumnDirective>
                 <ColumnDirective field='CustomerID' width='150'></ColumnDirective>
                 <ColumnDirective field='ShipCity' width='100'></ColumnDirective>
                 <ColumnDirective field='ShipName' width='150'></ColumnDirective>
                </ColumnsDirective>
               </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

Toggle selection

The Toggle selection allows to perform selection and unselection of the particular row or cell. To enable toggle selection, set enableToggle property of the selectionSettings as true. If you click on the selected row or cell then it will be unselected and vice versa.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, ColumnsDirective, ColumnDirective, SelectionSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from '../datasource.ts';

class App extends React.Component<{}, {}>{
    public data: Object[] = data;
    public settings: SelectionSettingsModel = { enableToggle: true};
    render() {
        return <GridComponent dataSource={this.data} height={315} selectionSettings={this.settings}>
                <ColumnsDirective>
                 <ColumnDirective type='checkbox' width='50'></ColumnDirective>
                 <ColumnDirective field='OrderID' width='120' textAlign="Right"></ColumnDirective>
                 <ColumnDirective field='CustomerID' width='150'></ColumnDirective>
                 <ColumnDirective field='ShipCity' width='100'></ColumnDirective>
                 <ColumnDirective field='ShipName' width='150'></ColumnDirective>
                </ColumnsDirective>
               </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

If multi selection is enabled, then first click on any selected row (without pressing Ctrl key), it will clear the multi selection and in second click on the same row, it will be unselected.

Select row at Initial rendering

To select a row at initial rendering, set the selectedRowIndex value.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, ColumnsDirective, ColumnDirective } from '@syncfusion/ej2-react-grids';
import { data } from '../datasource.ts';

class App extends React.Component<{}, {}>{
    public data: Object[] = data;
    public settings: SelectionSettingsModel = { type: 'Multiple', mode: 'Both' };
    render() {
        return <GridComponent dataSource={this.data} selectedRowIndex={1} selectionSettings={this.settings} height={315}>
                <ColumnsDirective>
                 <ColumnDirective field='OrderID' width='120' textAlign="Right"></ColumnDirective>
                 <ColumnDirective field='CustomerID' width='150'></ColumnDirective>
                 <ColumnDirective field='ShipCity' width='100'></ColumnDirective>
                 <ColumnDirective field='ShipName' width='150'></ColumnDirective>
                </ColumnsDirective>
               </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

Get Selected Row Indexes

You can get the selected row indexes by using the getSelectedRowIndexes method.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, ColumnsDirective, ColumnDirective } from '@syncfusion/ej2-react-grids';
import { data } from '../datasource.ts';

class App extends React.Component<{}, {}>{
    private grid: GridComponent;
    public data: Object[] = data;
    public rowSelected() {
        let selectedrowindex: number[] = this.grid.getSelectedRowIndexes();  // Get the selected row indexes.
        alert(selectedrowindex); // To alert the selected row indexes.
        let selectedrecords: Object[] = this.grid.getSelectedRecords();  // Get the selected records.
    }
    render() {
        return <GridComponent dataSource={this.data} height={315} rowSelected={this.rowSelected.bind(this)} ref={g => this.grid = g}>
                <ColumnsDirective>
                 <ColumnDirective field='OrderID' width='120' textAlign="Right"></ColumnDirective>
                 <ColumnDirective field='CustomerID' width='150'></ColumnDirective>
                 <ColumnDirective field='ShipCity' width='100'></ColumnDirective>
                 <ColumnDirective field='ShipName' width='150'></ColumnDirective>
                </ColumnsDirective>
               </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

Touch Interaction

When you tap a grid row on touchscreen device, the tapped row is selected. It also shows a popup Selection for multi-row selection. To select multiple rows or cells, tap the popupMulti Selection and then tap the desired rows or cells.

For multi-selection, It requires the selection type to be Multiple.

The following screenshot represents a grid touch selection in the device.

Touch Interaction

Multiple Selection based on condition

You can select multiple grid rows based on condition by using the selectRows method.

In the following code, the rows which contains ShipCountry value as Brazil are selected at initial rendering.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, ColumnsDirective, ColumnDirective } from '@syncfusion/ej2-react-grids';
import { sdata } from '../datasource.ts';

class App extends React.Component<{}, {}>{
    private grid: GridComponent;
    public data: Object[] = sdata;
    public settings: SelectionSettingsModel = { type: 'Multiple' };
    public dataBound() {
      let rowIndexes : number[]=[];
      this.grid.dataSource.forEach((data,index)=>{
          if(data.ShipCountry === "Brazil"){
            rowIndexes.push(index);
            }
            });
        this.grid.selectionModule.selectRows(rowIndexes);
    }
    render() {
        return <GridComponent dataSource={this.data} selectionSettings={this.settings} dataBound={this.dataBound.bind(this)} ref={g => this.grid = g}>
                <ColumnsDirective>
                 <ColumnDirective field='OrderID' width='120' textAlign="Right"></ColumnDirective>
                 <ColumnDirective field='CustomerID' width='150'></ColumnDirective>
                 <ColumnDirective field='ShipCountry' width='100'></ColumnDirective>
                 <ColumnDirective field='ShipName' width='150'></ColumnDirective>
                </ColumnsDirective>
               </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

Simple Multiple Row selection

You can select multiple rows by clicking on rows one by one. This will not deselect the previously selected rows. To deselect the previously selected row, you can click on the selected row. You can enable this behavior by using selectionSettings.enableSimpleMultiRowSelection property.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, ColumnsDirective, ColumnDirective, SelectionSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from '../datasource.ts';

class App extends React.Component<{}, {}>{
    public data: Object[] = data;
    public settings: SelectionSettingsModel = { type: 'Multiple',  enableSimpleMultiRowSelection: true };
    render() {
        return <GridComponent dataSource={this.data} selectionSettings={this.settings} height={315}>
                <ColumnsDirective>
                 <ColumnDirective field='OrderID' width='120' textAlign="Right"></ColumnDirective>
                 <ColumnDirective field='CustomerID' width='150'></ColumnDirective>
                 <ColumnDirective field='ShipCity' width='100'></ColumnDirective>
                 <ColumnDirective field='ShipName' width='150'></ColumnDirective>
                </ColumnsDirective>
               </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));