Row selection in Angular Grid component
31 Aug 202424 minutes to read
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
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:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit } from '@angular/core';
import { data } from './datasource';
import { SelectionSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template:`
<ejs-grid #grid [dataSource]="data" allowPaging="true"
[selectionSettings]="selectionOptions">
<e-columns>
<e-column field="OrderID" headerText="Order ID" width="120" textAlign="Right">
</e-column>
<e-column field="CustomerID" headerText="Customer Name" width="150">
</e-column>
<e-column field="OrderDate" headerText="Order Date" width="130" format="yMd"
textAlign="Right"></e-column>
<e-column field="Freight" headerText="Freight" width="120" format="C2"
textAlign="Right"></e-column>
<e-column field="ShippedDate" headerText="Shipped Date" width="130"
format="yMd" textAlign="Right"></e-column>
<e-column field="ShipCountry" headerText="Ship Country" width="150">
</e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent {
public data?: Object[];
public selectionOptions?: SelectionSettingsModel;
public ngOnInit(): void {
this.data = data;
this.selectionOptions = { mode: 'Row', type: 'Single' };
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Multiple row selection
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:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit } from '@angular/core';
import { data } from './datasource';
import { SelectionSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<ejs-grid #grid [dataSource]="data" allowPaging="true"
[selectionSettings]="selectionOptions">
<e-columns>
<e-column field="OrderID" headerText="Order ID" width="120" textAlign="Right">
</e-column>
<e-column field="CustomerID" headerText="Customer Name" width="150">
</e-column>
<e-column field="OrderDate" headerText="Order Date" width="130" format="yMd"
textAlign="Right"></e-column>
<e-column field="Freight" headerText="Freight" width="120" format="C2"
textAlign="Right"></e-column>
<e-column field="ShippedDate" headerText="Shipped Date" width="130"
format="yMd" textAlign="Right"></e-column>
<e-column field="ShipCountry" headerText="Ship Country" width="150">
</e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent {
public data?: Object[];
public selectionOptions?: SelectionSettingsModel;
public ngOnInit(): void {
this.data = data;
this.selectionOptions = { mode: 'Row', type: 'Multiple' };
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Select row at initial rendering
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:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit } from '@angular/core';
import { data } from './datasource';
import { SelectionSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `<ejs-grid [dataSource]='data' [selectedRowIndex]=1
[selectionSettings]='selectionOptions' height='315px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right'
width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150>
</e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent implements OnInit {
public data?: object[];
public selectionOptions?: SelectionSettingsModel;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { type: 'Multiple', mode: 'Row' };
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Select rows in any page based on index value
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:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import {GridModule, PageService, EditService, ToolbarService, FilterService, SelectionService } from '@syncfusion/ej2-angular-grids'
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component, OnInit, ViewChild } from '@angular/core';
import { EditEventArgs, GridComponent } from '@syncfusion/ej2-angular-grids';
import { data } from './datasource';
import { ChangeEventArgs } from '@syncfusion/ej2-dropdowns';
@Component({
imports: [
GridModule,
DropDownListModule
],
providers: [EditService, ToolbarService, PageService, FilterService, SelectionService],
standalone: true,
selector: 'app-root',
template: `<div style="display: flex">
<label style="padding: 30px 20px 0 0" > Select Row :</label>
<ejs-dropdownlist id='value' style="padding: 26px 0 0 0" #sample index='0'
width='220' [dataSource]='ddlData' (change)='valueChange($event)' >
</ejs-dropdownlist>
</div>
<div style="padding: 20px 17px 0 0">
<ejs-grid #grid [dataSource]='data'
(actionComplete)='actioncomplete($event)' allowPaging='true'
height=365 [pageSettings]='initialPage'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120'
textAlign='Right'></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'>
</e-column>
<e-column field='OrderDate' headerText='Order Date' width='130'
format="yMd" textAlign='Right'></e-column>
<e-column field='Freight' headerText='Freight' width='120'
format='C2' textAlign='Right'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' width='170'>
</e-column>
</e-columns>
</ejs-grid>
</div>`
})
export class AppComponent implements OnInit {
public data?: Object[];
public initialPage?: Object;
public mod: any;
public value: any;
@ViewChild('grid')
public grid?: GridComponent;
public ddlData?: 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' }
];
ngOnInit(): void {
this.data = data;
this.initialPage = { pageSize: 10 }
}
actioncomplete(args: EditEventArgs): void {
if (args.requestType === "paging") {
(this as any).grid.selectRow(this.mod);
}
}
valueChange(args: ChangeEventArgs): void {
this.value = +args.value;
this.mod = (this.value - 1) % 10;
const page = Math.ceil(this.value / 10);
if (page === 1) {
if ((this.grid as GridComponent).pagerModule.pagerObj.currentPage != 1) {
(this.grid as GridComponent).pagerModule.goToPage(1);
}
(this.grid as GridComponent).selectRow(this.mod);
}
else {
(this.grid as GridComponent).pagerModule.goToPage(page);
if ((this.grid as GridComponent).pagerModule.pagerObj.currentPage == page) {
(this.grid as GridComponent).selectRow(this.mod);
}
}
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Multiple row selection by single click on row
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:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { SwitchModule } from '@syncfusion/ej2-angular-buttons'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { GridComponent, SelectionSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule,
SwitchModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<div style="padding: 0px 0px 20px 0px">
<label>Enable/Disable simple multiple row selection</label>
<ejs-switch id="switch" [(checked)]="enableSelection"
(change)="toggleRowSelection()"></ejs-switch>
</div>
<ejs-grid #grid [dataSource]="data" [selectionSettings]="selectionOptions"
height="315px">
<e-columns>
<e-column field="OrderID" headerText="Order ID" textAlign="Right"
width="120"></e-column>
<e-column field="CustomerID" headerText="Customer ID" width="150"></e-column>
<e-column field="ShipCity" headerText="Ship City" width="150"></e-column>
<e-column field="ShipName" headerText="Ship Name" width="150"></e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent implements OnInit {
public data?: object[];
public selectionOptions?: SelectionSettingsModel;
public enableSelection = false;
@ViewChild('grid') grid!: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { type: 'Multiple' };
}
toggleRowSelection(): void {
this.grid.selectionSettings.enableSimpleMultiRowSelection = this.enableSelection;
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Select rows externally
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
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:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { TextBoxModule } from '@syncfusion/ej2-angular-inputs'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import {
GridComponent,
SelectionSettingsModel,
PageSettingsModel,
} from '@syncfusion/ej2-angular-grids';
import { TextBoxComponent } from '@syncfusion/ej2-angular-inputs';
@Component({
imports: [
GridModule,
TextBoxModule,
ButtonModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<div>
<label style="padding: 30px 17px 0 0">Enter the row index: </label>
<ejs-textbox #textbox required width="220">
</ejs-textbox>
</div>
<div style="padding: 10px 0 0px 150px">
<button ejs-button id="button" (click)="click()">Select Row</button>
</div>
<div style="padding: 20px 0px 0px 0px">
<ejs-grid #grid [dataSource]="data" [selectionSettings]="selectionOptions" >
<e-columns>
<e-column field="OrderID" headerText="Order ID" textAlign="Right"
width="120"></e-column>
<e-column field="CustomerID" headerText="Customer ID" width="120"></e-column>
<e-column field="ShipCountry" headerText="Ship Country" width="130">
</e-column>
<e-column field="Freight" headerText="Freight" format="C2" width="100">
</e-column>
</e-columns>
</ejs-grid>
</div>`
})
export class AppComponent implements OnInit {
public data?: Object[];
public selectionOptions?: SelectionSettingsModel;
public pageOptions?: PageSettingsModel;
public rowIndex?: number;
@ViewChild('textbox') public textbox?: TextBoxComponent;
@ViewChild('grid') public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { mode: 'Row', type: 'Single' };
this.pageOptions = { pageSize: 5 };
}
click(): void {
this.rowIndex = parseInt((this.textbox as TextBoxComponent).element.value, 10);
if (!isNaN(this.rowIndex)) {
(this.grid as GridComponent).selectRow(this.rowIndex);
}
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Multiple rows selection
The Angular 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.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { TextBoxModule } from '@syncfusion/ej2-angular-inputs'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import {
GridComponent,
SelectionSettingsModel,
PageSettingsModel,
} from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule,
TextBoxModule,
ButtonModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<div style="padding: 10px 0px 20px 0px">
<button ejs-button class="btn" (click)="selectRows([1, 3])">
select [1, 3] </button>
<button ejs-button class="btn" (click)="selectRows([0, 2])">
select [0, 2]</button>
<button ejs-button class="btn" (click)="selectRows([2, 4])">
select [2, 4] </button>
<button ejs-button class="btn" (click)="selectRows([0,5])">
select [0,5]</button>
<button ejs-button class="btn" (click)="selectRows([1,6])">
select [1,6]</button>
</div>
<div style="padding: 10px 0px 20px 0px">
<button ejs-button class="btn" (click)="selectRows([0,7,8])">
select [0,7,8]</button>
<button ejs-button class="btn" (click)="selectRows([1,9,10])">
select [1,9,10]</button>
<button ejs-button class="btn" (click)="selectRows([4,7,12])">
select [4,7,12]</button>
<button ejs-button class="btn" (click)="selectRows([2,5,6])">
select [2,5,6]</button>
</div>
<div style="padding: 20px 0px 0px 0px">
<ejs-grid #grid [dataSource]="data" [selectionSettings]="selectionOptions">
<e-columns>
<e-column field="OrderID" headerText="Order ID" textAlign="Right"
width="120"></e-column>
<e-column field="CustomerID" headerText="Customer ID" width="120">
</e-column>
<e-column field="ShipCountry" headerText="Ship Country" width="130">
</e-column>
<e-column field="Freight" headerText="Freight" format="C2" width="100">
</e-column>
</e-columns>
</ejs-grid>
</div>`
})
export class AppComponent implements OnInit {
public data?: Object[];
public selectionOptions?: SelectionSettingsModel;
public pageOptions?: PageSettingsModel;
@ViewChild('grid') public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { type: 'Multiple', mode: 'Row' };
this.pageOptions = { pageSize: 5 };
}
selectRows(rowIndexes: number[]): void {
this.grid?.clearRowSelection();
this.grid?.selectRows(rowIndexes);
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Range of rows selection
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.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { TextBoxModule } from '@syncfusion/ej2-angular-inputs'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import {
GridComponent,
SelectionSettingsModel,
PageSettingsModel,
} from '@syncfusion/ej2-angular-grids';
import { TextBoxComponent } from '@syncfusion/ej2-angular-inputs';
@Component({
imports: [
GridModule,
TextBoxModule,
ButtonModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<div>
<label style="padding: 30px 17px 0 0">Enter the start row index: </label>
<ejs-textbox #textbox required width="120"></ejs-textbox>
</div>
<div>
<label style="padding: 30px 17px 0 0">Enter the end row index: </label>
<ejs-textbox #textbox1 required width="120"></ejs-textbox>
</div>
<div style="padding: 10px 0 0px 180px">
<button ejs-button id="button" (click)="click()">Select rows</button>
</div>
<div style="padding: 20px 0px 0px 0px">
<ejs-grid #grid [dataSource]="data" [selectionSettings]="selectionOptions">
<e-columns>
<e-column field="OrderID" headerText="Order ID" textAlign="Right"
width="120"></e-column>
<e-column field="CustomerID" headerText="Customer ID" width="120"></e-column>
<e-column field="ShipCountry" headerText="Ship Country" width="130"></e-column>
<e-column field="Freight" headerText="Freight" format="C2" width="100">
</e-column>
</e-columns>
</ejs-grid>
</div>`
})
export class AppComponent implements OnInit {
public data?: Object[];
public selectionOptions?: SelectionSettingsModel;
public pageOptions?: PageSettingsModel;
public startIndex?:number;
public endIndex?:number;
@ViewChild('textbox') public textbox?: TextBoxComponent;
@ViewChild('textbox1') public textbox1?: TextBoxComponent;
@ViewChild('grid') public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { mode: 'Row', type: 'Multiple' };
this.pageOptions = { pageSize: 5 };
}
click(): void {
this.startIndex = parseInt((this.textbox as TextBoxComponent).element.value, 10);
this.endIndex = parseInt((this.textbox1 as TextBoxComponent).element.value, 10);
(this.grid as GridComponent).selectionModule.clearRowSelection();
if (!isNaN(this.startIndex) && !isNaN(this.endIndex)) {
(this.grid as GridComponent).selectionModule.selectRowsByRange(this.startIndex, this.endIndex);
}
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Select grid rows based on certain condition
You can programmatically select specific rows in the Angular 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 and rowDataBound along with obtaining the index value based on the condition.
In the below demo, we have selected the grid rows only when EmployeeID column value greater than 3.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule } from '@syncfusion/ej2-angular-grids'
import { PageService, SortService, FilterService, GroupService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data, columnDataType } from './datasource';
import { GridComponent, SelectionSettingsModel,RowDataBoundEventArgs } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule
],
providers: [PageService,
SortService,
FilterService,
GroupService],
standalone: true,
selector: 'app-root',
template: `<div id="GridParent">
<ejs-grid #Grid [dataSource]="data" allowPaging='true' [selectionSettings]='selectionOptions'
(rowDataBound)='rowDataBound($event)' (dataBound)='dataBound()' height='273px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' isPrimaryKey='true' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='EmployeeID' headerText='Employee ID' textAlign= 'Right' width=120></e-column>
<e-column field='ShipCity' headerText='Ship City' width=120></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>`
})
export class AppComponent implements OnInit {
public data?: object[];
@ViewChild('Grid')
public grid?: GridComponent;
public selectionOptions?: SelectionSettingsModel;
public selIndex?: number[] = [];
ngOnInit(): void {
this.data = data;
this.selectionOptions = { type: 'Multiple' };
}
public rowDataBound(args: RowDataBoundEventArgs ): void {
if (((args.data as columnDataType)['EmployeeID'] as number) > 3) {
this.selIndex?.push(parseInt(((args.row as Element).getAttribute('aria-rowindex') as string), 10));
}
}
public dataBound(): void {
if (this.selIndex?.length) {
this.grid?.selectRows(this.selIndex);
this.selIndex = [];
}
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
How to get selected row indexes
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:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { GridComponent, SelectionSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule,
ButtonModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<div style="padding: 10px 0px 20px 0px">
<button ejs-button class="btn" (click)="click()">Get selected row indexes</button>
</div>
<p id="message" *ngIf="showMessage">Selected row indexes: {{ selectedRowIndexes }}</p>
<ejs-grid #grid [dataSource]="data" height="315px"
[selectionSettings]="selectionOptions">
<e-columns>
<e-column field="OrderID" headerText="Order ID" textAlign="Right"
width="120"></e-column>
<e-column field="CustomerID" headerText="Customer ID" width="150">
</e-column>
<e-column field="ShipCity" headerText="Ship City" width="150">
</e-column>
<e-column field="ShipName" headerText="Ship Name" width="150">
</e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent implements OnInit {
public data?: object[];
public selectedRowIndexes: number[] = [];
public selectionOptions?: SelectionSettingsModel;
public showMessage = false;
@ViewChild('grid')
public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { type: 'Multiple' };
}
click() {
this.selectedRowIndexes = (this.grid as GridComponent).getSelectedRowIndexes();
this.showMessage = this.selectedRowIndexes.length > 0;
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
How to get selected records on various pages
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 persistselection 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:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import {GridModule, PageService, EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { DialogModule } from '@syncfusion/ej2-angular-popups'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { GridComponent, SelectionSettingsModel, PageSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule,
ButtonModule,
DialogModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<div style="padding: 20px 0px">
<button ejs-button class="sample" (click)="showSelectedRecords()">Show Selected Records</button>
</div>
<ejs-grid #grid [dataSource]="data" allowPaging="true" [selectionSettings]="selectionOptions"
[pageSettings]="pageOptions">
<e-columns>
<e-column type="checkbox" width="50"></e-column>
<e-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right"
width="120"></e-column>
<e-column field="CustomerID" headerText="Customer ID" width="120"></e-column>
<e-column field="ShipCountry" headerText="Ship Country" width="130"></e-column>
<e-column field="Freight" headerText="Freight" format="C2" width="100"></e-column>
</e-columns>
</ejs-grid>
<ejs-dialog #dialogComponent [header]="'Selected Records'" [content]="dialogContent"
[visible]="dialogVisible" (close)="dialogClose()" showCloseIcon="true" width="400px"
[position]='{ X: 300, Y: 100 }'>
<ng-template #dialogContent>
<ng-container>
<div *ngFor="let record of selectedRecords">
<p><b>Order ID:</b> {{ record.OrderID }}</p>
</div>
</ng-container>
</ng-template>
</ejs-dialog>
`
})
export class AppComponent implements OnInit {
public data?: object[];
public selectionOptions?: SelectionSettingsModel;
public pageOptions?: PageSettingsModel;
public dialogVisible: boolean = false;
public selectedRecords: Order[] = [];
@ViewChild('grid')
public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { type: 'Multiple', persistSelection: true };
this.pageOptions = { pageSize: 5 };
}
showSelectedRecords(): void {
this.selectedRecords = (this.grid as GridComponent).getSelectedRecords();
if (this.selectedRecords.length > 0) {
this.dialogVisible = true;
}
}
dialogClose(): void {
this.dialogVisible = false;
}
}
interface Order {
OrderID?: number;
CustomerID?: string;
ShipCountry?: string;
Freight?: number;
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
To persist the grid selection, it is necessary to define any one of the columns as a primary key using the columns property.
How to get selected records
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:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { GridComponent, SelectionSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule,
ButtonModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<div style="padding: 20px 0px">
<button ejs-button class="sample" (click)="click()">Selected Records count</button>
</div>
<p id="message" *ngIf="showMessage">Selected record count: {{ selectedRecordscount }}</p>
<div class="control-section">
<ejs-grid #grid [dataSource]="data" allowPaging="true" [allowSelection]="true"
[selectionSettings]="selectionOptions">
<e-columns>
<e-column field="OrderID" isPrimaryKey="true" headerText="Order ID" width="120"
textAlign="Right"></e-column>
<e-column field="CustomerID" headerText="Customer ID" width="150"></e-column>
<e-column field="OrderDate" headerText="Order Date" width="130" format="yMd"
textAlign="Right"></e-column>
<e-column field="Freight" headerText="Freight" width="120" format="C2"
textAlign="Right"></e-column>
<e-column field="ShipCountry" headerText="Ship Country" width="130" format="yMd"
textAlign="Right"></e-column>
</e-columns>
</ejs-grid>
</div>
`,
})
export class AppComponent implements OnInit {
@ViewChild('grid') public grid?: GridComponent;
public data?: Object[];
public selectionOptions?: SelectionSettingsModel;
public showMessage = false;
public selectedRecordscount: number = 0;
public ngOnInit(): void {
this.data = data;
this.selectionOptions = { type: 'Multiple' };
}
click(): void {
this.selectedRecordscount = (this.grid as GridComponent).getSelectedRecords().length;
this.showMessage = this.selectedRecordscount > 0;
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Clear row selection programmatically
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
clearRowSelection
method 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.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { TextBoxModule } from '@syncfusion/ej2-angular-inputs'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { GridComponent, SelectionSettingsModel, PageSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule,
TextBoxModule,
ButtonModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `<div style="padding: 20px 0px 0px 0px">
<button ejs-button id="button" (click)='click()'>clear Row Selection</button>
</div>
<div style="padding: 20px 0px 0px 0px">
<ejs-grid #grid [dataSource]='data' [selectedRowIndex]=2 allowPaging=true
[selectionSettings]='selectionOptions' [pageSettings]='pageOptions'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right'
width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120>
</e-column>
<e-column field='ShipCountry' headerText='Ship Country' width=130>
</e-column>
<e-column field='Freight' headerText='Freight' format= 'C2'
width=100></e-column>
</e-columns>
</ejs-grid>
</div>`
})
export class AppComponent implements OnInit {
public data?: Object[];
public selectionOptions?: SelectionSettingsModel;
public pageOptions?: PageSettingsModel;
@ViewChild('grid')
public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { mode: 'Row', type: 'Multiple' };
this.pageOptions = { pageSize: 5 };
}
click(): void {
(this.grid as GridComponent).selectionModule.clearRowSelection()
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Row selection events
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.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, PageService } from '@syncfusion/ej2-angular-grids'
import { EditService, ToolbarService, FilterService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit, ViewChild, Renderer2 } from '@angular/core';
import { data ,Order} from './datasource';
import {
GridComponent,
SelectionSettingsModel,
PageSettingsModel,
RowSelectingEventArgs,
RowSelectEventArgs,
RowDeselectEventArgs,
RowDeselectingEventArgs,
} from '@syncfusion/ej2-angular-grids';
import { DialogComponent } from '@syncfusion/ej2-angular-popups';
@Component({
imports: [
GridModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<p id="message">{{ message }}</p>
<div style="padding: 20px 0px 0px 0px">
<ejs-grid
#grid
[enableHover]='false'
[dataSource]="data"
[selectionSettings]="selectionOptions"
(rowSelected)="rowSelected($event)"
(rowSelecting)="rowselecting($event)"
(rowDeselected)="rowDeselected($event)"
(rowDeselecting)="rowDeselecting($event)">
<e-columns>
<e-column field="OrderID" headerText="Order ID" textAlign="Right" width="120">
</e-column>
<e-column field="CustomerID" headerText="Customer ID" width="120">
</e-column>
<e-column field="ShipCountry" headerText="Ship Country" width="130">
</e-column>
<e-column field="Freight" headerText="Freight" format="C2" width="100">
</e-column>
</e-columns>
</ejs-grid>
</div>`,
})
export class AppComponent implements OnInit {
public data?: Object[];
public selectionOptions?: SelectionSettingsModel;
public pageOptions?: PageSettingsModel;
public message?: string;
@ViewChild('grid') public grid?: GridComponent;
@ViewChild('dialogComponent') public dialog?: DialogComponent;
public selectedCellCount: number = 0;
public dialogVisible: boolean = false;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { mode: 'Row', type: 'Multiple' };
this.pageOptions = { pageSize: 5 };
}
rowselecting(args: RowSelectingEventArgs): void {
this.message = `Trigger rowSelecting`;
if ((args.data as Order).CustomerID == 'VINET')
args.cancel = true;
}
rowSelected(args: RowSelectEventArgs): void {
this.message = ` Trigger rowSelected`;
if ((args.data as Order).Freight > 10 || (args.data as Order).Freight <= 140)
(args.row as HTMLElement).style.backgroundColor = 'rgb(96, 158, 101)';
}
rowDeselected(args: RowDeselectEventArgs ): void {
this.message = `Trigger rowDeselected`;
if ((args.data as Order).Freight <= 10)
(args.row as HTMLElement).style.backgroundColor = 'red';
}
rowDeselecting(args: RowDeselectingEventArgs): void {
this.message = `Trigger rowDeselecting`;
if ((args.data as Order).Freight > 140)
(args.row as HTMLElement).style.backgroundColor = 'yellow';
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));