Command Column Editing in Angular Data Grid
5 Sep 202610 minutes to read
The command column feature provides built‑in action buttons within a dedicated column, enabling direct interaction with individual rows. These commands can be configured for editing, saving, and deleting records, as well as for custom operations such as viewing details or integrating external actions. This approach streamlines row‑level interactions and ensures a consistent experience within the grid interface.
Enable command column editing
Command column editing is activated through the commands property. This property specifies which command buttons display in the command column, such as Edit, Delete, Save, and Cancel.
To enable command column functionality, include the CommandColumnService module in the component’s providers array. This service supports the required logic for handling command column actions.
import { Component } from '@angular/core';
import { GridComponent, EditService, CommandColumnService } from '@syncfusion/ej2-angular-grids';
@Component({
selector: 'app-root',
template: `<ejs-grid [dataSource]='data'>
<!-- Grid configuration -->
</ejs-grid>`,
providers: [EditService, CommandColumnService]
})
export class AppComponent {
public data: any[] = [...];
}The following built-in command buttons are available:
| Command button | Actions |
|---|---|
Edit |
Enables inline editing for the current row. |
Delete |
Removes the current row from the grid. |
Save |
Updates changes made to the edited row. |
Cancel |
Discards changes and exits edit mode. |
The following example demonstrates CRUD action buttons in a command column using the commands property:
import { data } from './datasource';
import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { CommandColumnService, CommandModel, EditService, EditSettingsModel, GridModule } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [ GridModule, CommonModule ],
providers: [EditService, CommandColumnService],
standalone: true,
selector: 'app-root',
template: `<ejs-grid [dataSource]='data' [editSettings]='editSettings' height='310px'>
<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='Freight' headerText='Freight' textAlign= 'Right'
editType= 'numericedit' width=120 format= 'C2'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' editType= 'dropdownedit' width=150></e-column>
<e-column headerText='Commands' width=120 [commands]='commands'></e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent implements OnInit {
public data?: object[];
public editSettings?: EditSettingsModel;
public commands?: CommandModel[];
ngOnInit(): void {
this.data = data;
this.editSettings = { allowEditing: true, allowDeleting: true };
this.commands = [{ type: 'Edit', buttonOption: { cssClass: 'e-flat', iconCss: 'e-edit e-icons' } },
{ type: 'Delete', buttonOption: { cssClass: 'e-flat', iconCss: 'e-delete e-icons' } },
{ type: 'Save', buttonOption: { cssClass: 'e-flat', iconCss: 'e-update e-icons' } },
{ type: 'Cancel', buttonOption: { cssClass: 'e-flat', iconCss: 'e-cancel-icon e-icons' } }];
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Custom command column button
Custom command columns provide additional row-level actions beyond the built-in command buttons. This is particularly powerful when specialized functionality for editing, deletion, or custom operations is required beyond the built-in command set.
To define custom command buttons, use the column.commands property. Associate the desired actions with these buttons through the commandClick event, allowing custom logic to be executed on button click.
The following example demonstrates custom command buttons using the commands property and custom click behavior through the commandClick event, which is used here to show row details in a dialog:
import { GridModule, EditService, CommandColumnService } from '@syncfusion/ej2-angular-grids';
import { DialogModule } from '@syncfusion/ej2-angular-popups';
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { CommandModel, CommandClickEventArgs, GridComponent, Column, IRow, EditSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [GridModule, DialogModule],
providers: [EditService, CommandColumnService],
standalone: true,
selector: 'app-root',
template: `
<ejs-grid #grid [dataSource]="data" [editSettings]="editSettings" (commandClick)="commandClick($event)" height="310px">
<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="Freight" headerText="Freight" textAlign="Right" editType="numericedit" width="120" format="C2"></e-column>
<e-column field="ShipCountry" headerText="Ship Country" editType="dropdownedit" width="150"></e-column>
<e-column headerText="Commands" width="140" [commands]="commands"></e-column>
</e-columns>
</ejs-grid>
<ejs-dialog #dialog header="Row Information" [content]="dialogContent" showCloseIcon="true"
width="400px" [visible]="dialogVisible" (close)="dialogClose()">
<ng-template #dialogContent>
<ng-container *ngIf="rowData">
<p><b>ShipName:</b> {{ rowData.ShipName }}</p>
<p><b>ShipPostalCode:</b> {{ rowData.ShipPostalCode }}</p>
<p><b>ShipAddress:</b> {{ rowData.ShipAddress }}</p>
</ng-container>
</ng-template>
</ejs-dialog>`
})
export class AppComponent implements OnInit {
public data?: object[];
public editSettings?: EditSettingsModel;
public commands?: CommandModel[];
@ViewChild('grid') public grid?: GridComponent;
public rowData: any;
public dialogVisible: boolean = false;
ngOnInit(): void {
this.data = data;
this.editSettings = { allowEditing: true, allowDeleting: true };
this.commands = [{ buttonOption: { content: 'Details', cssClass: 'e-flat' } }];
}
public commandClick(args: CommandClickEventArgs): void {
this.rowData = args.rowData;
if (this.rowData) {
this.dialogVisible = true;
}
}
public 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));The
commandClickevent is triggered when a custom command button is clicked and provides access to the current row data.