# Reactive Forms

The ListView provides options for editing its data by using the [Reactive Angular Forms](https://angular.dev/guide/forms/reactive-forms).

## Basic Concepts

To implement the editing mode of the Kendo UI ListView for Angular with Reactive Forms:

1. Define the [`EditTemplateDirective`](https://www.telerik.com/kendo-angular-ui/components/listview/api/edittemplatedirective) for the ListView items. 1. Call the [`addItem`](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent#addItem) or the [`editItem`](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent#editItem) method respectively.
2. Pass the `FormGroup` property as one of its parameters. The [`FormGroup`](https://angular.dev/api/forms/FormGroup) configuration is part of the Angular Forms package.

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

## Setup

> The editing of the ListView in template-driven forms is based on the item index. As a result, the Template Forms immediately update the value.

To enable the editing mode of the ListView in Angular Reactive Forms:

1. Pass an [editing template](https://www.telerik.com/kendo-angular-ui/components/listview/api/edittemplatedirective).

   ```html
   <kendo-listview>
       <ng-template kendoListViewEditTemplate let-formGroup="formGroup" let-index="index">
           <input type="text" formControlName="ProductName"/>
       </ng-template>
   </kendo-listview>
   ```

2. Configure the commands. You need to define the command buttons inside the editing template.

   ```html
   <kendo-listview>
       <ng-template kendoListViewEditTemplate let-formGroup="formGroup" let-index="index" let-isNew="isNew">
           <input type="text" formControlName="ProductName"/>
           <button kendoListViewSaveCommand>{{ isNew ? 'Add' : 'Update' }}</button>
           <button kendoListViewCancelCommand>{{ isNew ? 'Discard changes' : 'Cancel' }}</button>
       </ng-template>
   </kendo-listview>
   ```

3. Attach handlers for CRUD data operations.

   When the user clicks a command button, the ListView emits the corresponding event. To instruct the component what action to perform, handle the event that is emitted.

   ```html
   <kendo-listview
     [data]="view | async"
     (edit)="editHandler($event)" (cancel)="cancelHandler($event)"
     (save)="saveHandler($event)" (remove)="removeHandler($event)"
     (add)="addHandler($event)"
   >
   
   </kendo-listview>
   ```

## Toggling the Edit State

Inside the corresponding event handlers, you can toggle the edit state of the ListView by using the following events:

* [`edit`](https://www.telerik.com/kendo-angular-ui/components/listview/editing/editing-reactive-forms#editing-records)
* [`add`](https://www.telerik.com/kendo-angular-ui/components/listview/editing/editing-reactive-forms#adding-records)
* [`cancel`](https://www.telerik.com/kendo-angular-ui/components/listview/editing/editing-reactive-forms#cancelling-editing)
* [`save`](https://www.telerik.com/kendo-angular-ui/components/listview/editing/editing-reactive-forms#saving-records)
* [`remove`](https://www.telerik.com/kendo-angular-ui/components/listview/editing/editing-reactive-forms#removing-records)

### Editing Records

The [`edit`](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent#edit) event fires when the `kendoListViewEditCommand` is clicked. Inside the event handler, you can toggle the item's editing mode by calling the [`editItem`](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent#editItem) method.

```ts
public editHandler({sender, itemIndex, dataItem}) {
    // Close the previously edited item.
    this.closeEditor(sender);

    // Track the most recently edited item.
    // It will be used in `closeEditor` for closing the previously edited item.
    this.editedItemIndex = itemIndex;

    // Clone the current - `[(ngModel)]` will modify the original item.
    // Use this copy to revert changes.
    this.editedProduct = Object.assign({}, dataItem);

    // Edit the item.
    sender.editItem(itemIndex);
}
```

### Adding Records

The [`add`](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent#add) event fires when the `kendoListViewAddCommand` is clicked. Inside the event handler, you can show the new item editor by calling the [`addItem`](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent#addItem) method.

```ts
public addHandler({sender}) {
    // Close the previously edited item.
    this.closeEditor(sender);

    // Define validators and default values for editable fields.
    this.formGroup = new FormGroup({
        'ProductID': new FormControl(),
        'ProductName': new FormControl("New product", Validators.required)
    });

    // Pass the FormGroup to the editor for the new item.
    sender.addItem(this.formGroup);
}
```

### Cancelling Editing

The [`cancel`](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent#cancel) event fires when the `kendoListViewCancelCommand` is clicked. Inside the event handler, you can switch the item back to the view mode by calling the [`closeItem`](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent#closeitem) method.

```ts
public cancelHandler({sender, itemIndex}) {
    // Close the item editor and restore the previous state.
    this.closeEditor(sender, itemIndex);
}

private closeEditor(sender, itemIndex = this.editedItemIndex) {
    // Close the editor before restoring the original data.
    sender.closeItem(itemIndex);

    // Revert the data item to the original state.
    this.editService.resetItem(this.editedProduct);

    // Clear the edited item state after closing the editor.
    this.editedItemIndex = undefined;
    this.editedProduct = undefined;
}
```

### Saving Records

The [`save`](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent#save) event fires when the `kendoListViewSaveCommand` is clicked.

```ts
public saveHandler({sender, itemIndex, formGroup, isNew}) {
    const product: Product = formGroup.value;

    // Update the data source.
    this.editService.save(product, isNew);

    // Close the editor after saving the item.
    sender.closeItem(itemIndex);
}
```

### Removing Records

The [`remove`](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent#remove) event fires when the `kendoListViewRemoveCommand` is clicked. Inside the event handler, you can issue a request to remove the current data item from the data source.

```ts
public removeHandler({dataItem}) {
    // Remove the current data item through the service that owns the data source.
    this.editService.remove(dataItem);
}
```

## Suggested Links

* [Overview of Editing](https://www.telerik.com/kendo-angular-ui/components/listview/editing)
* [Editing in Template Forms](https://www.telerik.com/kendo-angular-ui/components/listview/editing/editing-template-forms)
* [API Index of the ListView](https://www.telerik.com/kendo-angular-ui/components/listview/api)
* [API Reference of the ListViewComponent](https://www.telerik.com/kendo-angular-ui/components/listview/api/listviewcomponent)
