# Binding the Angular Grid to Remote Data

You can bind the [Angular Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid) to server-side (remote) data and display the data in tabular form. This approach is suitable for scenarios that involve remote data services and server-side data operations.

The Kendo Angular Grid has representational functionality and isn't affected by the way how the data gets updated and where it comes from. This allows you to use any remote data source. Once the data is received from the server, it can be loaded in two ways. Choose the appropriate approach based on the specific configuration requirements:

* [Use manual binding through the `data` property](https://www.telerik.com/kendo-angular-ui-develop/components/grid/data-binding/remote-data#manual-data-management)—Provides more control when implementing data operations.
* [Provide a custom data-binding directive](https://www.telerik.com/kendo-angular-ui-develop/components/grid/data-binding/remote-data#using-a-remote-binding-directive)—Suitable for abstracting away the communication with the remote data service.

## Manual Data Management

The manual binding provides the most straightforward way of binding an array of items to the Grid. To bind the Grid to remote data:

1. Create the necessary remote requests.
2. Set the [`data`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#data) input property of the component. Once the data is received from an asynchronous source, pipe it through the `async` pipe.

   ```html
   <kendo-grid [data]="gridData | async" >
       <kendo-grid-column field="CompanyName"></kendo-grid-column>
       <kendo-grid-column field="ContactName"></kendo-grid-column>
   </kendo-grid>
   ```

The following example demonstrates binding the Grid `data` property to an `Observable` that streams records from a remote service.

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

Using the manual binding approach allows you to take full control over the data operations applied to the Grid. To implement custom logic while modifying the data:

1. Handle the respective [data operation events](https://www.telerik.com/kendo-angular-ui-develop/components/grid/data-operations) triggered when the user interacts with the Grid.
2. Modify the received descriptors in the event handlers.
3. Initiate the necessary remote requests.
4. Provide the received data set back to the Grid. As a result, the Grid will reflect the changes and update the UI.

Depending on the enabled data operations, you can bind the [`data`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#data) property to:

* [`GridDataResult`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/griddataresult) object—when [paging](https://www.telerik.com/kendo-angular-ui-develop/components/grid/paging/basics) or [virtual scrolling](https://www.telerik.com/kendo-angular-ui-develop/components/grid/scroll-modes/virtual) is enabled.
* `Array`—when [paging](https://www.telerik.com/kendo-angular-ui-develop/components/grid/paging/basics) or [virtual scrolling](https://www.telerik.com/kendo-angular-ui-develop/components/grid/scroll-modes/virtual) is not applied.

### Binding to GridDataResult Object

To improve the Grid performance when the data contains a lot of records, utilize the [paging](https://www.telerik.com/kendo-angular-ui-develop/components/grid/paging/basics) or [virtual scrolling](https://www.telerik.com/kendo-angular-ui-develop/components/grid/scroll-modes/virtual) features. In such scenarios, bind the [`data`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#data) property of the component to a [`GridDataResult`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/griddataresult) object.

The following example demonstrates using `GridDataResult` to supply data records and total count to the Grid for server-driven paging.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/data-binding/remote-data/grid-data-result/source.txt)

## Using a Remote-Binding Directive

To encapsulate the data operations logic and the requests to the server, you have to implement a custom directive that extends the built-in [`DataBindingDirective`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/databindingdirective).

A remote data-binding directive is suitable for reducing the boilerplate code, required for the communication between the Grid and the remote data service. The directive can be also reused across multiple Grids throughout the application as necessary.

The following step-by-step guide shows how to create a remote data-binding directive.

1. Extend the built-in [`DataBindingDirective`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/databindingdirective) of the Grid. This enables you to reuse the state persistance.

   ```ts
       @Directive({
           selector: '[productsBinding]'
       })
       export class ProductsBindingDirective extends DataBindingDirective {}
   ```

2. Use the DI to provide:

   * The instance of the data service that will be used to fetch the data.
   * The instance of the Grid component—used to update the data and optionally toggle the built-in loading indicator.

   ```ts
       constructor(
           private productsService: ProductsService,
           grid: GridComponent) {
           super(grid);
       }
   ```

3. Inside the `OnInit` hook, subscribe to the appropriate service observable that will return the remote server responses and assign the received data set to the [`data`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#data) property of the Grid.

   By default, the Grid does not trigger an event when it receives a new data set from the server. Use the [`notifyDataChange`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/databindingdirective#notifydatachange) method of the `DataBindingDirective` to notify the Grid that the data is changed.

   ```ts
   private serviceSubscription: Subscription;

   public ngOnInit(): void {
       this.serviceSubscription = this.productsService.subscribe((result) => {
           this.grid.loading = false; // Stop the indicator after the remote result arrives.
           this.grid.data = result;
           this.notifyDataChange(); // Remote responses do not trigger Grid data-operation events.
       });

       super.ngOnInit(); // Subscribe to Grid state changes before the initial query.

       this.rebind(); // Load initial data after the directive starts tracking Grid state.
   }
   ```

4. Use the `rebind` function to pass the current Grid [`State`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/state) to a dedicated data service method that returns the updated collection. In this case, the `ProductsService` extends the `NorthwindService` which has a `query` method that is responsible for the communication with the remote server. The `query` method passes the information required for server-side data operations like paging and sorting

   ```ts
       public rebind(): void {
           this.grid.loading = true; // Keep the indicator active until the response clears it.

           this.productsService.query(this.state);
       }
   ```

5. Use the newly created `productsBinding` directive on the Grid.

   ```html
   <kendo-grid
       productsBinding
       [pageSize]="10"
       [pageable]="true"
       [sortable]="true">
   </kendo-grid>
   ```

The following example demonstrates extending the built-in `DataBindingDirective` to create a custom directive that forwards sorting, filtering, and paging state to a remote service.

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

## Indicating Ongoing Data Operations

Loading indicators inform the user about ongoing data operations.

You can show a loading indicator by:

* Setting the built-in [`loading`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#loading) property of the Grid. For an example, see the [`Remote-Binding Directive`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/data-binding/remote-data#using-a-remote-binding-directive) section.
* Using the [`Grid Loading Template`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/accessories/loading-template).
* Utilizing the [`Kendo UI for Angular Loader`](https://www.telerik.com/kendo-angular-ui-develop/components/indicators/loader) component.

The following example demonstrates overlaying a Kendo UI Loader on the Grid as a visual loading indicator while remote data is being fetched.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/data-binding/remote-data/loading-indicator/source.txt)

## Suggested Links

* [DataBindingDirective](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/databindingdirective)
* [API Reference of the GridComponent](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent)
* [API Index of the Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api)
