# Angular Data Grid Tracking Changes

The [Angular Grid](https://www.telerik.com/kendo-angular-ui/components/grid) tracks changes by the index of the data item while the edited rows are tracked by reference.

The [`trackBy`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#trackby) callback allows you to override the default behavior and provide your own logic of how data item changes will be tracked.

## Tracking Changes by Index

The following example demonstrates how to track items only by index. This is especially useful when you want to avoid rerendering the detail row template when changes occur in master row.

```html
<kendo-grid [data]="gridData" [trackBy]="trackByIndex"> </kendo-grid>
```

```ts
public trackByIndex(index: number, item: GridItem): any {
    return index;
}
```

## Tracking Changes by a Data Item Field

To track Grid changes by a data item, use the [`GridItem`](https://www.telerik.com/kendo-angular-ui/components/grid/api/griditem) which is the second argument of the `trackBy` callback.

The following example demonstrates how to track changes by items.

```html
<kendo-grid [data]="gridData" [trackBy]="trackByItem"> </kendo-grid>
```

```ts
public trackByItem(index: number, item: GridItem): any {
    return item.data.ProductID;
}
```

Compare the effect of using a unique identifier with `trackBy` versus the default behavior by typing in the input field and sorting the Grid in the example below.

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

## Understanding trackBy Performance Benefits in Kendo UI for Angular Grid

The `trackBy` function enhances the Kendo UI for Angular Grid's efficiency by identifying changes in the data array. Without `trackBy`, the Grid tracks items by their index or reference of the edited row, which may lead to unexpected results in some cases, for example when using templates alongside data operations like sorting.

### When to Use trackBy?

Use `trackBy` when:

* Handling large datasets.
* Frequently updating data.
* Optimizing performance by reducing DOM operations.
* Managing complex templates, detail rows, or editing scenarios.
* Unexpected editing behavior occurs due to the default tracking mechanism.

### Common Pitfalls

Avoid these mistakes when using `trackBy`:

* Using non-unique or mutable properties for tracking.
* Performing complex computations within the function.
* Relying on index-based tracking when unique identifiers exist.

### Key Benefits of trackBy

1. Performance Optimization—Prevents unnecessary re-renders by tracking items with a unique identifier (e.g., an `id`).

2. Efficient Change Detection—Updates only modified items instead of re-rendering the entire list, enhancing performance for large or frequently updated data sets.

3. Lower Memory Usage—Reduces redundant DOM updates, improving responsiveness and resource efficiency.

4. Maintains Row State—Preserves row states (e.g., selection, editing) when sorting, filtering, or editing items.

### Practical Scenarios

1. Dynamic Data Updates—Ensures smooth updates by re-rendering only changed items, ideal for real-time data feeds.

2. Large Data Sets—Avoids full re-renders, maintaining performance when only a few items change.

3. Editable Grid's—Preserves row states by re-rendering only modified rows, improving efficiency.

4. Pagination & Virtual Scrolling—Optimizes rendering as users navigate or scroll through the Grid.

5. Cell Templates—Maintains custom cell states or styles without re-rendering the entire Grid. For example, if a cell contains a text input or a checkbox and the Grid is sorted, the cell state is preserved.

## Suggested Links

* [Angular Grid Editing Basics](https://www.telerik.com/kendo-angular-ui/components/grid/editing/basics)
* [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)
* [Angular TrackByFunction](https://angular.io/api/core/TrackByFunction)
