Cell Selection in Angular Grid component
5 Nov 202524 minutes to read
Cell selection in the Grid component enables interactive selection of specific cells or ranges of cells within the grid. You can select cells using mouse clicks or arrow keys (up, down, left, right). This is valuable for highlighting, manipulating, or performing operations on particular grid cells.
To enable cell selection, set selectionSettings.mode to Cell or Both.
Single cell selection
Single cell selection allows selection of one cell at a time. To configure this, set selectionSettings.mode to Cell and selectionSettings.type to Single.
The following example demonstrates single cell selection:
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 } 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: 'Cell', type: 'Single' };
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Multiple cell selection
Multiple cell selection permits selection of multiple cells at once. Set selectionSettings.mode to Cell and selectionSettings.type to Multiple.
The following example demonstrates how to enable multiple cell selection:
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 } 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: 'Cell', type: 'Multiple' };
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Cell selection mode
Cell selection mode controls how cells or ranges are selected. Set the desired mode using selectionSettings.cellSelectionMode:
- Flow (default): Selects a continuous flow of cells between the start and end indices across rows.
- Box: Selects a rectangular range covering specified rows and columns.
- BoxWithBorder: Similar to Box mode, but applies a border for better visual distinction of the selected range.
For cell selection modes,
selectionSettings.modemust be Cell or Both, and type must be Multiple.
You can dynamically update cellSelectionMode with a DropDownList component, as shown in this example:
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 { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { CellSelectionMode, GridComponent, SelectionSettingsModel } from '@syncfusion/ej2-angular-grids';
import { ChangeEventArgs } from '@syncfusion/ej2-dropdowns';
@Component({
imports: [
GridModule,
DropDownListModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<div style="display: flex ">
<label style="padding: 30px 17px 0 0" >Choose cell selection mode :</label>
<ejs-dropdownlist style="padding: 26px 0 0 0" index="0" width="150"
[dataSource]=" dropdownData" (change)="valueChange($event)">
</ejs-dropdownlist>
</div>
<div style="padding: 20px 0px 0px 0px">
<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>
</div>`
})
export class AppComponent implements OnInit {
public data?: object[];
@ViewChild('grid')
public grid?: GridComponent;
public selectionOptions?: SelectionSettingsModel;
public dropdownData: Object[] = [
{ text: 'Flow', value: 'Flow' },
{ text: 'Box', value: 'Box' },
{ text: 'BoxWithBorder', value: 'BoxWithBorder' },
];
ngOnInit(): void {
this.data = data;
this.selectionOptions = { type: 'Multiple', mode: 'Cell' };
}
valueChange(args: ChangeEventArgs): void {
((this.grid as GridComponent).selectionSettings.cellSelectionMode as CellSelectionMode) = (args.value as CellSelectionMode);
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Select cells externally
You can programmatically select single cells, multiple cells, or a range of cells using built-in methods. This is useful for custom UI interactions or workflows.
Single cell selection
Select a cell by index using the selectCell method.
The example below shows selecting a cell from the input (row and cell index):
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="120"></ejs-textbox>
</div>
<div>
<label style="padding: 30px 17px 0 0">Enter the cell index: </label>
<ejs-textbox #textbox1 required width="120"></ejs-textbox>
</div>
<div style="padding: 10px 0 0px 10%">
<button ejs-button id="button" (click)="click()">Select Cell</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;
public cellIndex?: number;
@ViewChild('textbox') public textbox?: TextBoxComponent;
@ViewChild('textbox1') public textbox1?: TextBoxComponent;
@ViewChild('grid') public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { mode: 'Cell', type: 'Single' };
this.pageOptions = { pageSize: 5 };
}
click(): void {
this.rowIndex = parseInt((this.textbox as TextBoxComponent).element.value, 10);
this.cellIndex = parseInt((this.textbox1 as TextBoxComponent).element.value, 10);
if (!isNaN(this.rowIndex) && !isNaN(this.cellIndex)) {
(this.grid as GridComponent).selectCell({ rowIndex: this.rowIndex, cellIndex: this.cellIndex });
}
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Multiple cell selection
Select several cells by providing row and column indexes using selectCells.
The following example enables multiple cell selection programmatically:
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 } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule,
TextBoxModule,
ButtonModule
],
providers: [EditService, ToolbarService, PageService, FilterService],
standalone: true,
selector: 'app-root',
template: `
<div style="padding: 0px 0px 20px 0px">
<button ejs-button class="btn" (click)="buttonClick(1, 3)">
Select [1, 3]</button>
<button ejs-button class="btn" (click)="buttonClick(2, 2)">
Select [2, 2]</button>
<button ejs-button class="btn" (click)="buttonClick(0, 0)">
Select [0, 0]</button>
</div>
<div style="padding: 0px 0px 20px 0px">
<button ejs-button class="btn" (click)="buttonClick(4, 2)">
Select [4, 2]</button>
<button ejs-button class="btn" (click)="buttonClick(5, 1)">
Select [5, 1]</button>
<button ejs-button class="btn" (click)="buttonClick(3, 3)">
Select [3, 3]</button>
</div>
<div style="padding: 0px 0px 20px 0px">
<button ejs-button class="btn" (click)="buttonClick(0, 3)">
Select [0, 3]</button>
<button ejs-button class="btn" (click)="buttonClick(8, 1)">
Select [8, 1]</button>
<button ejs-button class="btn" (click)="buttonClick(8, 2)">
Select [8, 2]</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;
@ViewChild('grid') public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { type: 'Multiple', mode: 'Cell' };
}
buttonClick(rowIndex: number, columnIndex: number): void {
this.grid?.selectCells([{ rowIndex: rowIndex, cellIndexes: [columnIndex] }]);
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));For multiple cell selection and range selection, set
selectionSettings.modeto Cell or Both and type to Multiple.
Range of cell selection
Select a continuous cell range using selectCellsByRange by supplying start and end row/cell indexes.
In Box mode, range selection highlights a boxed area. In Flow mode, the range selection includes every cell within the row range between start and end.
SetselectionSettings.modeto Cell or Both and type to Multiple.
Below is an example that demonstrates selecting a cell range using values from the input:
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 0px 0px">Enter the start column index: </label>
<ejs-textbox #textbox1 required width="120"></ejs-textbox>
</div>
<div>
<label style="padding: 30px 17px 0 0">Enter the end row index: </label>
<ejs-textbox #textbox2 required width="120"></ejs-textbox>
</div>
<div>
<label style="padding: 30px 17px 0 0px">Enter the end column index: </label>
<ejs-textbox #textbox3 required width="120"></ejs-textbox>
<button ejs-button id="button" (click)="click()">Select range of Cell</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 startRowIndex?:number;
public startColumnIndex?:number;
public endRowIndex?:number;
public endColumnIndex?:number;
@ViewChild('textbox') public textbox?: TextBoxComponent;
@ViewChild('textbox1') public textbox1?: TextBoxComponent;
@ViewChild('textbox2') public textbox2?: TextBoxComponent;
@ViewChild('textbox3') public textbox3?: TextBoxComponent;
@ViewChild('grid') public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { type: 'Multiple', mode: 'Cell' };
this.pageOptions = { pageSize: 5 };
}
click(): void {
this.startRowIndex = parseInt((this.textbox as TextBoxComponent).value, 10);
this.startColumnIndex = parseInt((this.textbox1 as TextBoxComponent).value, 10);
this.endRowIndex = parseInt((this.textbox2 as TextBoxComponent).value, 10);
this.endColumnIndex = parseInt((this.textbox3 as TextBoxComponent).value, 10);
this.grid?.clearCellSelection();
if (!isNaN(this.startRowIndex) && !isNaN(this.startColumnIndex) && !isNaN(this.endRowIndex) && !isNaN(this.endColumnIndex)) {
(this.grid as GridComponent).selectCellsByRange({ rowIndex: this.startRowIndex, cellIndex: this.startColumnIndex }, { rowIndex: this.endRowIndex, cellIndex: this.endColumnIndex });
}
}
}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 cell indexes
You can retrieve all currently selected cell indexes using the getSelectedRowCellIndexes method. This is useful for processing or displaying information about selected row and cell coordinates.
Below is an example demonstrating how to obtain selected cell indexes and display them in a dialog:
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 { DialogModule } from '@syncfusion/ej2-angular-popups'
import { CommonModule } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { GridComponent, SelectionSettingsModel, PageSettingsModel, ISelectedCell } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
CommonModule,
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)="showSelectedIndexes()">
Show Selected Cell Indexes</button>
</div>
<ejs-grid #grid [dataSource]="data" allowPaging="true"
[selectionSettings]="selectionOptions" [pageSettings]="pageOptions">
<e-columns>
<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 Cell Indexes'" [content]="dialogContent"
[visible]="dialogVisible" (close)="dialogClose()" showCloseIcon="true" width="350px"
[position]='{ X: 300, Y: 100 }'>
<ng-template #dialogContent>
<ng-container>
<div>
<p><b>Selected Cell Indexes:</b></p>
<ul>
<li *ngFor="let cellIndex of selectedCellIndexes">
Row: {{ cellIndex.rowIndex }}, cellIndex: {{ cellIndex.cellIndexes }}
</li>
</ul>
</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 selectedCellIndexes: ISelectedCell[] = [];
@ViewChild('grid')
public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { mode:'Cell', type: 'Multiple', persistSelection: true };
this.pageOptions = { pageSize: 5 };
}
showSelectedIndexes(): void {
this.selectedCellIndexes = (this.grid as GridComponent).getSelectedRowCellIndexes();
if (this.selectedCellIndexes.length > 0) {
this.dialogVisible = true;
}
}
dialogClose(): void {
this.dialogVisible = false;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Clear cell selection programmatically
Clear any current cell selections using clearCellSelection. This is useful for resetting or preparing the grid for new selections.
clearCellSelectionis supported for both Multiple and Single selection types.
Example of programmatic cell selection clearing:
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, PageSettingsModel } 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 0px 0px">
<button ejs-button id="button" (click)='click()'>Clear cell selection</button>
</div>
<div style="padding: 20px 0px 0px 0px">
<ejs-grid #grid [dataSource]='data' 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 = { type: 'Multiple', mode: 'Cell' };
this.pageOptions = { pageSize: 5 };
}
click(): void {
(this.grid as GridComponent).clearCellSelection();
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Cell selection events
Several events help you customize cell selection interactions:
- cellSelecting: Triggered before a cell selection, allowing logic or validation to control selection.
- cellSelected: Triggered after cell selection is completed.
- cellDeselecting: Triggered before a selected cell is deselected, enabling custom logic or cancellation.
- cellDeselected: Triggered after a selected cell is deselected.
In the example below:
- Cell selection is blocked via
cellSelectingif ShipCountry equals France. - Background color updates on selection/deselection events to illustrate the event life cycle.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit, ViewChild, Renderer2 } from '@angular/core';
import { Order, data } from './datasource';
import { GridComponent, SelectionSettingsModel,PageSettingsModel,CellSelectEventArgs,CellSelectingEventArgs,
CellDeselectEventArgs} from '@syncfusion/ej2-angular-grids';
@Component({
imports: [ GridModule],
standalone: true,
selector: 'app-root',
template: `
<p id="message">{{ message }}</p>
<div style="padding: 20px 0px 0px 0px">
<ejs-grid #grid [dataSource]="data"
[selectionSettings]="selectionOptions"
(cellSelected)="cellSelected($event)"
(cellSelecting)="cellselecting($event)"
(cellDeselected)="cellDeselected($event)"
(cellDeselecting)="cellDeselecting($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;
ngOnInit(): void {
this.data = data;
this.selectionOptions = { mode: 'Cell', type: 'Multiple' };
this.pageOptions = { pageSize: 5 };
}
cellSelected(args: CellSelectEventArgs): void {
this.message = ` Trigger cellSelected`;
(args.currentCell as HTMLElement).style.backgroundColor = 'rgb(96, 158, 101)';
}
cellselecting(args:CellSelectingEventArgs ): void {
this.message = `Trigger cellSelecting`;
if ((args.data as Order).ShipCountry == 'France')
args.cancel = true;
}
cellDeselected(args: CellDeselectEventArgs ): void {
this.message = `Trigger cellDeselected`;
if (args.cells && args.cells.length > 0) {
(args.cells[0] as HTMLElement).style.backgroundColor = 'rgb(245, 69, 69)';
}
}
cellDeselecting(args: CellDeselectEventArgs): void {
this.message = `Trigger cellDeselecting`;
if (args.cells && args.cells.length > 0) {
(args.cells[0] as HTMLElement).style.color = 'rgb(253, 253, 253)';
}
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));