Ignite UI for Angular Grid component provides a great data manipulation capabilities and powerful API for Angular CRUD operations. By default the Grid is using in cell editing and different editors will be shown based on the column data type, thanks to the default cell editing template. In addition, you can define your own custom templates for update-data actions and to override the default behavior for committing and discarding any changes.
最速で機能豊富な Angular Data Grid は、ページング、ソート、フィルタリング、グループ化、PDF および Excel へのエクスポートなどの機能を提供します。究極のアプリ構築エクスペリエンスとデータ操作に必要なすべてが揃っています。
Like this sample? Get access to our complete Ignite UI for Angular toolkit and start building your own apps in minutes. Download it for free.
By using igxCellEditor with any type of editor component, the keyboard navigation flow will be disrupted. The same applies to direct editing of the custom cell that enters edit mode. This is because the focus will remain on the cell element, not on the editor component that we've added - igxSelect, igxCombo, etc. This is why we should take leverage of the igxFocus directive, which will move the focus directly in the in-cell component and will preserve a fluent editing flow of the cell/row.
Cell Editing
Editing through UI
You can enter edit mode for specific cell, when an editable cell is focused in one of the following ways:
on double click;
on single click - Single click will enter edit mode only if the previously selected cell was in edit mode and currently selected cell is editable. If the previously selected cell was not in edit mode, single click will select the cell without entering edit mode;
on key press Enter;
on key press F2;
You can exit edit mode without committing the changes in one of the following ways:
on key press Escape;
when you perform sorting, filtering, searching and hiding operations;
You can exit edit mode and commit the changes in one of the following ways:
on key press Enter;
on key press F2;
on key press Tab;
on single click to another cell - when you click on another cell in the Grid, your changes will be submitted.
operations like paging, resize, pin or move will exit edit mode and changes will be submitted.
The cell remains in edit mode when you scroll vertically or horizontally or click outside the Grid. This is valid for both cell editing and row editing.
Editing through API
You can also modify the cell value through the IgxGrid API but only if primary key is defined:
Another way to update cell is directly through update method of IgxGridCell:
publicupdateCell() {
const cell = this.grid1.getCellByColumn(rowIndex, 'ReorderLevel');
// You can also get cell by rowID if primary key is defined// cell = this.grid1.getCellByKey(rowID, 'ReorderLevel');
cell.update(70);
}
typescript
Cell Editing Templates
You can see and learn more for default cell editing templates in the general editing topic.
If you want to provide a custom template which will be applied when a cell is in edit mode, you can make use of the igxCellEditor directive. To do this, you need to pass an ng-template marked with the igxCellEditor directive and properly bind your custom control to the cell.editValue:
<igx-columnfield="class"header="Class" [editable]="true"><ng-templateigxCellEditorlet-cell="cell"let-value><igx-selectclass="cell-select" [(ngModel)]="cell.editValue" [igxFocus]="true"><igx-select-item *ngFor="let class of classes" [value]="class">
{{ class }}
</igx-select-item></igx-select></ng-template></igx-column>html
This code is used in the sample below which implements an IgxSelectComponent in the cells of the Race, Class and Alignment columns.
Any changes made to the cell's editValue in edit mode, will trigger the appropriate editing event on exit and apply to the transaction state (if transactions are enabled).
The cell template igxCell controls how a column's cells are shown when outside of edit mode.
The cell editing template directive igxCellEditor, handles how a column's cells in edit mode are displayed and controls the edited cell's edit value.
By using igxCellEditor with any type of editor component, the keyboard navigation flow will be disrupted. The same applies to direct editing of the custom cell that enters edit mode. This is because the focus will remain on the cell element, not on the editor component that we've added - igxSelect, igxCombo, etc. This is why we should take leverage of the igxFocus directive, which will move the focus directly in the in-cell component and will preserve a fluent editing flow of the cell/row.
For more information on how to configure columns and their templates, you can see the documentation for Grid Columns configuration.
Grid Excel Style Editing
Using Excel Style Editing allows the user to navigate trough the cells just as he would using the Excel, and ever so quickly edit them.
Implementing this custom functionality can be done by utilizing the events of the grid. First we hook up to the grid's keydown events, and from there we can implement two functionalities:
if (key == 13) {
let thisRow = activeElem.row;
const column = activeElem.column;
const rowInfo = grid.dataView;
// to find the next eiligible cell, we will use a custom method that will check the next suitable indexlet nextRow = this.getNextEditableRowIndex(thisRow, rowInfo, event.shiftKey);
// and then we will navigate to it using the grid's built in method navigateTothis.grid.navigateTo(nextRow, column, (obj) => {
obj.target.activate();
this.grid.clearCellSelection();
this.cdr.detectChanges();
});
}
typescript
Key parts of finding the next eligible index would be:
//first we check if the currently selected cell is the first or the lastif (currentRowIndex < 0 || (currentRowIndex === 0 && previous) || (currentRowIndex >= dataView.length - 1 && !previous)) {
return currentRowIndex;
}
// in case using shift + enter combination, we look for the first suitable cell going up the fieldif(previous){
return dataView.findLastIndex((rec, index) => index < currentRowIndex && this.isEditableDataRecordAtIndex(index, dataView));
}
// or for the next one down the fieldreturn dataView.findIndex((rec, index) => index > currentRowIndex && this.isEditableDataRecordAtIndex(index, dataView));
typescript
Please check the full sample for further reference:
Constant edit mode: typing while a cell is selected will immediately enter edit mode with the value typed, replacing the existing one
Any non-data rows are skipped when navigating with Enter/Shift+Enter. This allows users to quickly cycle through their values.
CRUD operations
Please keep in mind that when you perform some CRUD operation all of the applied pipes like filtering, sorting and grouping will be re-applied and your view will be automatically updated.
The IgxGridComponent provides a straightforward API for basic CRUD operations.
Adding a new record
The Grid component exposes the addRow method which will add the provided data to the data source itself.
// Adding a new record// Assuming we have a `getNewRecord` method returning the new row data.const record = this.getNewRecord();
this.grid.addRow(record);
typescript
Updating data in the Grid
Updating data in the Grid is achieved through updateRow and updateCell methods but only if primary key for the grid is defined. You can also directly update a cell and/or a row value through their respective update methods.
// Updating the whole rowthis.grid.updateRow(newData, this.selectedCell.cellID.rowID);
// Just a particular cell through the Grid APIthis.grid.updateCell(newData, this.selectedCell.cellID.rowID, this.selectedCell.column.field);
// Directly using the cell `update` methodthis.selectedCell.update(newData);
// Directly using the row `update` methodconst row = this.grid.getRowByKey(rowID);
row.update(newData);
typescript
Deleting data from the Grid
Please keep in mind that deleteRow() method will remove the specified row only if primary key is defined.
// Delete row through Grid APIthis.grid.deleteRow(this.selectedCell.cellID.rowID);
// Delete row through row objectconst row = this.grid.getRowByIndex(rowIndex);
row.delete();
typescript
These can be wired to user interactions, not necessarily related to the igx-grid; for example, a button click:
Using the grid's editing events we can alter how the user interacts with the grid.
In this example, we'll validate a cell based on the data entered in it by binding to the cellEdit event. If the new value of the cell does not meet our predefined criteria, we'll prevent it from reaching the data source by cancelling the event (event.cancel = true). We'll also display a custom error message using IgxToast.
The first thing we need to is bind to the grid's event:
The cellEdit emits whenever any cell's value is about to be committed. In our handleCellEdit definition, we need to make sure that we check for our specific column before taking any action:
exportclassMyGridEventsComponent{
public handleCellEdit(event: IGridEditEventArgs): void {
const column = event.column;
if (column.field === 'Ordered') {
const rowData = event.rowData;
if (!rowData) {
return;
}
if (event.newValue > rowData.UnitsInStock) {
event.cancel = true;
this.toast.open();
}
}
}
}
typescript
If the value entered in a cell under the Ordered column is larger than the available amount (the value under Units in Stock), the editing will be cancelled and a toast with an error message will be displayed.
The result of the above validation being applied to our igx-grid can be seen in the below demo:
<h4>Shipping Orders</h4><igx-grid [igxPreventDocumentScroll]="true" [data]="products"height="500px"primaryKey="ProductID" (cellEdit)="handleCellEdit($event)"><igx-columnfield="ProductName"header="Product Name"dataType="string"></igx-column><igx-columnfield="UnitPrice"header="Price"dataType="number" [editable]="true"></igx-column><igx-columnfield="UnitsInStock"header="Units In Stock"dataType="number" [editable]="true"></igx-column><igx-columnfield="Ordered"header="Ordered"dataType="number" [editable]="true"></igx-column></igx-grid><igx-toast>You cannot order more than the units in stock!</igx-toast>html
The IgxGrid allows for its cells to be styled through the Ignite UI for Angular Theme Library. The grid's grid-theme exposes a wide range of properties, which allow users to style many different aspects of the grid.
In the below steps, we are going to go over how you can style the grid's cell in edit mode and how you can scope those styles.
In order to use the Ignite UI Theming Library, we must first import the theme index file in our global styles:
Importing style library
@use"igniteui-angular/theming" as *;
// IMPORTANT: Prior to Ignite UI for Angular version 13 use:// @import '~igniteui-angular/lib/core/styles/themes/index';scss
Now we can make use of all of the functions exposed by the Ignite UI for Angular theme engine.
Defining a palette
After we've properly imported the index file, we create a custom palette that we can use. Let's define three colors that we like and use them to build a palette with palette: