# Angular Data Grid Inline Editing

The inline editing mode in the [Angular Grid](https://www.telerik.com/kendo-angular-ui/components/grid) allows users to click an `Edit` command button to switch an entire row into edit mode. This type of editing is suitable when you want to edit all cells of a given row in a single transaction.

The Grid offers flexible options for implementing inline editing—from quick setup with built-in directives that handle the operations automatically, to manual implementation for scenarios requiring complete control over the editing process.

The following example demonstrates the quick setup for implementing inline editing using [Reactive Forms](https://angular.dev/guide/forms/reactive-forms).

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/editing/editing-directives/reactive-editing/source.txt)

## Quick Setup

The Grid's built-in editing directives provide the fastest and most straightforward way to implement inline editing. These directives handle all the necessary Grid events automatically, reducing boilerplate code and providing a clean, maintainable solution that integrates seamlessly with [Angular Forms](https://angular.dev/guide/forms).

Based on your preferred Angular Forms methodology, you can choose from the following built-in approaches:

* [Using Reactive Forms](https://www.telerik.com/kendo-angular-ui/components/grid/editing/inline-editing#using-reactive-forms)
* [Using Template-Driven Forms](https://www.telerik.com/kendo-angular-ui/components/grid/editing/inline-editing#using-template-driven-forms)

### Using Reactive Forms

The [`kendoGridReactiveEditing`](https://www.telerik.com/kendo-angular-ui/components/grid/api/reactiveeditingdirective) directive simplifies the setup when implementing an editable Grid with [Reactive Forms](https://angular.dev/guide/forms/reactive-forms). This directive handles all the necessary Grid events internally, reducing repetitive code and providing a cleaner, more maintainable solution.

To configure the Grid with the `ReactiveEditingDirective`:

1. Provide a callback which returns the [`FormGroup`](https://angular.dev/api/forms/FormGroup) for the edited item.

   ```html
   <kendo-grid [kendoGridReactiveEditing]="createFormGroup">
       ...
   </kendo-grid>
   ```

2. In the custom `createFormGroup` callback, create and return the `FormGroup` for the row model. For the newly added rows, create a new instance of the model. Add `Validators` to form controls that require validation.

   ```ts
   public formGroup: FormGroup = this.formBuilder.group({
       patientId: null,
       fullName: '',
       totalCharges: null,
       lastVisitDate: null,
       primaryDiagnosis: '',
       hasInsurance: false,
   });

   constructor(private formBuilder: FormBuilder) {
       this.createFormGroup = this.createFormGroup.bind(this);
   }

   public createFormGroup = (args: any): FormGroup => {
       const item = args.isNew ? new Patient() : args.dataItem;

       this.formGroup = this.formBuilder.group({
           patientId: item.patientId,
           fullName: [item.fullName, Validators.required],
           totalCharges: [item.totalCharges, Validators.required],
           ...
       });

       return this.formGroup;
   };
   ```

3. Use the built-in command or toolbar directives to create buttons that automatically perform the desired CRUD operations. For more details, refer to the [Editing Action Buttons](https://www.telerik.com/kendo-angular-ui/components/grid/editing/basics#editing-action-buttons) section.

> To display validation error messages in the edited row, refer to the [Grid Editing Validation](https://www.telerik.com/kendo-angular-ui/components/grid/editing/validation) article.

### Using Template-Driven Forms

The [`kendoGridTemplateEditing`](https://www.telerik.com/kendo-angular-ui/components/grid/api/templateeditingdirective) directive simplifies the setup when implementing an editable Grid with [Template-Driven Forms](https://angular.dev/guide/forms). Like the reactive editing directive, it handles the necessary Grid events internally and reduces boilerplate code.

The following example demonstrates the `kendoGridTemplateEditing` directive in action.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/editing/editing-directives/template-editing/source.txt)

To configure the Grid with the `TemplateEditingDirective`:

1. Use the [`EditTemplateDirective`](https://www.telerik.com/kendo-angular-ui/components/grid/api/edittemplatedirective) of the Grid to display any custom control when the row is in edit mode. Bind the `ngModel` directive of the custom control to the corresponding value by using the `dataItem` field provided by the template context.

   ```html
   <kendo-grid-column field="fullName" title="Patient Name">
       <ng-template kendoGridEditTemplate let-dataItem="dataItem">
       <kendo-textbox
           [(ngModel)]="dataItem.fullName"
           name="fullName"
           required
       ></kendo-textbox>
       </ng-template>
   </kendo-grid-column>
   ```

2. Provide a function that creates a new instance of the edited model.

   ```HTML
   <form novalidate #myForm="ngForm">
       <kendo-grid [kendoGridTemplateEditing]="createNewPatient">
       ...
       </kendo-grid>
   </form>
   ```

   ```TypeScript
   export class Patient {
       public patientId: number;
       public fullName = '';
       ...
   }

   public createNewPatient(): Patient {
       return new Patient();
   }
   ```

3. Use the built-in command or toolbar directives to create buttons that automatically perform the desired CRUD operations. For more details, refer to the [Editing Action Buttons](https://www.telerik.com/kendo-angular-ui/components/grid/editing/basics#editing-action-buttons) section.

## Manual Setup

For scenarios requiring full control over the editing behavior, you can manually handle the editing operations. This approach gives you complete flexibility but requires more code to implement. Common reasons to use the manual setup include:

* Saving changes to a remote service and showing a loading indicator during the request.
* Conditionally preventing edits based on business rules.
* Synchronizing edits with a custom local data cache.

> **Built-In Directives vs. Manual Setup**
>
> The Grid provides built-in editing directives that significantly reduce the amount of boilerplate code required for implementing inline editing. Consider using the [Reactive Editing Directive](https://www.telerik.com/kendo-angular-ui/components/grid/editing/inline-editing#using-reactive-forms) or [Template Editing Directive](https://www.telerik.com/kendo-angular-ui/components/grid/editing/inline-editing#using-template-driven-forms) before implementing the manual setup.

The manual implementation uses either Angular [Reactive Forms](https://angular.dev/guide/forms/reactive-forms) or [Template-Driven Forms](https://angular.dev/guide/forms). This gives you the choice between two approaches:

* [Manual Setup with Reactive Forms](https://www.telerik.com/kendo-angular-ui/components/grid/editing/inline-editing#manual-setup-with-reactive-forms)
* [Manual Setup with Template-Driven Forms](https://www.telerik.com/kendo-angular-ui/components/grid/editing/inline-editing#manual-setup-with-template-driven-forms)

### Manual Setup with Reactive Forms

The following example demonstrates how to manually implement an inline-editing Grid with `Reactive Forms`.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/editing/inline-editing/source.txt)

To manually configure the inline editing mode of the Grid when using [Reactive Forms](https://angular.dev/guide/forms/reactive-forms) in Angular:

1. Set the [`editor`](https://www.telerik.com/kendo-angular-ui/components/grid/api/columncomponent#editor) property of each column in accordance with the type of data that will be edited. The default is the `text` editor—a textbox for editing `string` values. Internally, the built-in column editors utilize the Angular Reactive Forms directives.

   ```html
   <kendo-grid-column
       field="totalCharges"
       title="Price"
       editor="numeric"
   ></kendo-grid-column>
   ```

2. Configure the [`CommandColumnComponent`](https://www.telerik.com/kendo-angular-ui/components/grid/api/commandcolumncomponent) by defining the built-in command directives for the required editing operations. For more details, check the [Editing Action Buttons](https://www.telerik.com/kendo-angular-ui/components/grid/editing/basics#editing-action-buttons) section.

   ```html
   <kendo-grid-command-column title="command">
       <ng-template kendoGridCellTemplate>
           <button kendoGridEditCommand >Edit</button>
           
       </ng-template>
   </kendo-grid-command-column>
   ```

3. When a button with an associated command directive is clicked, the Grid emits the corresponding event. Handle the events and use the necessary Grid editing methods to process the data manually.

   ```html
   <kendo-grid
       (edit)="editHandler($event)"
       (cancel)="cancelHandler($event)"
       (save)="saveHandler($event)"
       (remove)="removeHandler($event)"
       (add)="addHandler($event)"
   >
       
   </kendo-grid>
   ```

#### Editing Records

The [`edit`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#edit) event fires when a button with `kendoGridEditCommand` directive is clicked. Inside the event handler, put the row in editing mode by calling the [`editRow`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#editrow) method. Pass the row index and the `FormGroup` instance that contains form controls for each editable data item field.

```ts
protected editHandler(args: EditEvent): void {
    const group = new FormGroup({
        'patientId': new FormControl(args.dataItem.patientId),
        // TODO: add FormControl instances for the remaining editable fields.
    });

    args.sender.editRow(args.rowIndex, group);
}
```

#### Adding Records

The [`add`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#add) event fires when a button with `kendoGridAddCommand` directive is clicked. Inside the event handler, show the newly added row in editing mode by calling the [`addRow`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#addrow) method. Provide a `FormGroup` configuration based on the default values for a newly added data item.

```ts
public addHandler(args: AddEvent): void {
    const group = new FormGroup({
        'patientId': new FormControl(),
        // TODO: add FormControl instances for the remaining editable fields.
    });

    args.sender.addRow(group);
}
```

#### Canceling Editing

The [`cancel`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#cancel) event fires when a button with `kendoGridCancelCommand` directive is clicked. Inside the event handler, switch the row back to readonly mode by calling the [closeRow](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#closerow) method.

```ts
public cancelHandler(args: CancelEvent): void {
    ...
    args.sender.closeRow(args.rowIndex)
}
```

#### Saving Records

The [`save`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#save) event fires when the `kendoGridSaveCommand` is clicked. Inside the event handler, you have access to the latest form value. Use it to update the Grid data accordingly. Call the [`closeRow`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#closerow) method to switch the current row back to readonly mode.

```ts
public saveHandler(args: SaveEvent): void {
    ...
    sender.closeRow(rowIndex);
}
```

#### Removing Records

The [`remove`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#remove) event fires when the `kendoGridRemoveCommand` is clicked. Inside the event handler, remove the respective data item from the data source.

```ts
public removeHandler(args: RemoveEvent): void {
    this.editService.remove(args.dataItem);
}
```

### Manual Setup with Template-Driven Forms

The Grid provides an option for editing its data by using the Angular [Template-Driven Forms](https://angular.dev/guide/forms). This type of editing relies on binding all editable data item fields with a form control using [two-way `ngModel` binding](https://angular.io/guide/forms#bind-input-controls-to-data-properties).

The following example demonstrates how to manyally implement a Grid with inline editing using Template-Driven Forms.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/editing/inline-editing-template-forms/source.txt)

To manually configure the inline editing mode of the Grid with Template-Driven Forms:

1. Wrap the Grid component in a `form` tag.

   ```html
   <form novalidate #myForm="ngForm">
       <kendo-grid>...</kendo-grid>
   </form>
   ```

2. Use the [`EditTemplateDirective`](https://www.telerik.com/kendo-angular-ui/components/grid/api/edittemplatedirective) of the Grid to define the custom input control to be rendered when a row is being edited. The data item associated with the edited row is exposed in the template context as `dataItem`. Use the `ngModel` directive to bind the form control to the respective data item field.

   ```html
   <kendo-grid-column field="fullName" title="Patient Name">
       <ng-template kendoGridEditTemplate let-dataItem="dataItem">
           <kendo-textbox
               [(ngModel)]="dataItem.fullName"
               name="fullName"
               required
           ></kendo-textbox>
       </ng-template>
   </kendo-grid-column>
   ```

3. Configure the [`CommandColumnComponent`](https://www.telerik.com/kendo-angular-ui/components/grid/api/commandcolumncomponent) component by defining the built-in command directives for the required editing operations. For more details, check the [Editing Action Buttons](https://www.telerik.com/kendo-angular-ui/components/grid/editing/basics#editing-action-buttons) section.

   ```html
   <kendo-grid-command-column title="command">
       <ng-template kendoGridCellTemplate>
           <button kendoGridEditCommand >Edit</button>
           
       </ng-template>
   </kendo-grid-command-column>
   ```

4. When a button with an associated command directive is clicked, the Grid emits the corresponding event. Handle the events and use the necessary Grid editing methods to process the data manually.

   ```html
   <kendo-grid
       (edit)="editHandler($event)"
       (cancel)="cancelHandler($event)"
       (save)="saveHandler($event)"
       (remove)="removeHandler($event)"
       (add)="addHandler($event)"
   >
       
   </kendo-grid>
   ```

#### Editing Records

The [edit](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#edit) event fires when a button with `kendoGridEditCommand` directive is clicked. Inside the event handler, put the row in editing mode by calling the [editRow](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#editrow) method.

```ts
public editHandler(args: EditEvent): void {
    ...
    args.sender.editRow(rowIndex);
}
```

#### Adding Records

The [`add`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#add) event fires when a button with `kendoGridAddCommand` directive is clicked. Inside the event handler, show the new row in editing mode by calling the [`addRow`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#addrow) method.

```ts
public addHandler(args: AddEvent): void {
    ...
    args.sender.addRow(new Patient());
}
```

#### Canceling Editing

The [`cancel`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#cancel) event fires when a button with `kendoGridCancelCommand` directive is clicked. Inside the event handler, switch the row back to readonly mode by calling the [`closeRow`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#closerow) method.

```ts
public cancelHandler(args: CancelEvent): void {
    args.sender.closeRow(args.rowIndex);
}
```

#### Saving Records

The [`save`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#save) event fires when a button with `kendoGridSaveCommand` directive is clicked. Inside the event handler, save the data and return the row back to readonly mode by calling the [`closeRow`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#closerow) method.

```ts
public saveHandler(args: SaveEvent): void {
    ...
    args.sender.closeRow(rowIndex);
}
```

#### Removing Records

The [`remove`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#remove) event fires when a button with `kendoGridRemoveCommand` directive is clicked. Inside the event handler, remove the current data item from the data source.

```ts
public removeHandler(args: RemoveEvent): void {
    this.editService.remove(args.dataItem);
}
```

## Inline Editing on Row Click

The [Angular Grid](https://www.telerik.com/kendo-angular-ui/components/grid) supports inline editing triggered by clicking anywhere on a row. This approach requires a manual implementation that handles the [`cellClick`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#cellclick) event of the Grid to put the clicked row into edit mode, eliminating the need for users to locate and click a specific **Edit** button.

The following example demonstrates how to achieve inline editing in the Grid by clicking anywhere on a row.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/editing/inline-editing-row-click/source.txt)

To implement the inline editing through a row click:

1. Handle the [`cellClick`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#cellclick) event that contains references to both the index of the clicked row and the respective data item in its event data.

   ```html
   <kendo-grid
       id="productsGrid"
       (cellClick)="editClick($event)" ...>
   </kendo-grid>
   ```

2. Depending on the behavior you wish to achieve, handle the click action outside the Kendo UI Grid for Angular by applying custom logic—for example, to save the currently edited item, use the following approach:

   ```ts
   const matches = (el, selector) => (el.matches || el.msMatchesSelector).call(el, selector);


   @ViewChild(GridComponent) private grid: GridComponent;

   public ngOnInit(): void {
     this.view = this.service.products();

     this.docClickSubscription = this.renderer.listen('document', 'click', this.onDocumentClick.bind(this));
   }

   public ngOnDestroy(): void {
       // Remove the document listener to avoid retaining the component after destruction.
       this.docClickSubscription();
   }

   private onDocumentClick(e: any): void {
       if (this.formGroup && this.formGroup.valid &&
           !matches(e.target, '#productsGrid tbody *, #productsGrid .k-grid-toolbar .k-button')) {
           // Save when the click leaves the Grid body and toolbar.
           this.saveCurrent();
       }
   }

   private saveCurrent(): void {
       if (this.formGroup) {
           this.service.save(this.formGroup.value, this.isNew);
           this.closeEditor();
       }
   }

   private closeEditor(): void {
       this.grid.closeRow(this.editedRowIndex);

       this.isNew = false;
       this.editedRowIndex = undefined;
       this.formGroup = undefined;
   }
   ```

## Managing Focus

By default, when the user opens a row for editing, the Grid focuses the first available cell editor. To disable this behavior or focus the editor of a different cell, pass an [options](https://www.telerik.com/kendo-angular-ui/components/grid/api/editrowoptions) object to the [editRow](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#editrow) method with a [columnIndex](https://www.telerik.com/kendo-angular-ui/components/grid/api/editrowoptions#columnindex) or [skipFocus](https://www.telerik.com/kendo-angular-ui/components/grid/api/editrowoptions#skipfocus) set.

The following example demonstrates how to focus the editor which corresponds to the clicked cell in the Grid.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/editing/focus-clicked-cell-in-row-editing/source.txt)

## Suggested Links

* [Angular Grid Editing Basics](https://www.telerik.com/kendo-angular-ui/components/grid/editing/basics)
* [Grid Editing Validation](https://www.telerik.com/kendo-angular-ui/components/grid/editing/validation)
* [Setting Up Custom Editors](https://www.telerik.com/kendo-angular-ui/components/grid/editing/custom-editors)
* [API Reference of the `GridComponent`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent)
* [API Index of the Grid](https://www.telerik.com/kendo-angular-ui/components/grid/api)
