# Row Pagination

## Live example

{% hint style="success" %}
[Row Pagination](https://demos.dikesoft.com/dk-grid/row/pagination) live example.
{% endhint %}

## Allowing pagination

To allow the user to paginate the DikeGrid rows, you must provide an input property named <mark style="color:orange;">`allowPagination`</mark> at the grid scope.

{% code title="row-pagination.component.html" %}

```markup
<dike-grid id="grid-row-pagination" height="700px" #grid="dkgGrid"
    [allowPagination]="gridProperties.allowPagination"
    [pageSize]="gridProperties.pageSize">
</dike-grid>
```

{% endcode %}

{% hint style="info" %}
You can change the value of the <mark style="color:orange;">`allowPagination`</mark> property at **runtime**.
{% endhint %}

Open the [Floating Configuration Panel](https://docs.dikesoft.com/overview#floating-configuration-panel) and click on the **Allow Pagination** checkbox.

![Floating Configuration Panel - Pagination](https://3888584995-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpDxfe6pgRLqBLMQgZ0kG%2Fuploads%2F4rVJTuGq2Tse1P1FPZWE%2Fpagination-panel-conf.png?alt=media\&token=47cd5555-0451-4a04-95c1-b5b28a3ec573)

After enabling the pagination, you will see the DikeGrid **paginator** at the bottom of the content rows.

![Enabling Row Pagination](https://3888584995-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpDxfe6pgRLqBLMQgZ0kG%2Fuploads%2FjPINsr873JtGwY8UMX0r%2Frow-pagination-enabled.png?alt=media\&token=417ccd51-332b-463a-b9c5-e245c6a73119)

## Page size

By default, the page size is **50**. You can change this value by providing an input property named <mark style="color:orange;">`pageSize`</mark>.

As you can see in the previous code snippet, we have bound the <mark style="color:orange;">`pageSize`</mark> property to the `gridProperties` object. Therefore, you can open the [Floating Configuration Panel](https://docs.dikesoft.com/overview#floating-configuration-panel) and change the value for the **page size**.

## Pagination Row

Since the DikeGrid structure uses the **CSS Grid Layout** specification, the paginator is called **Pagination Row**. Indeed, the Pagination Row is the **template-row** definition where the paginator lives.

{% hint style="info" %}
You can change the **height** of the **Pagination Row**. For further details, see the [Grid Structure - Paginator](https://docs.dikesoft.com/fundamentals/grid-structure/paginator#changing-the-pagination-row-height) section.
{% endhint %}

For this section, we have bound the input property <mark style="color:orange;">`paginationRowHeight`</mark> to the related property from the `gridProperties` object.

{% code title="row-pagination.component.html" %}

```markup
<dike-grid id="grid-row-pagination" height="600px" #grid="dkgGrid"
    [paginationRowHeight]="gridProperties.paginationHeight">
</dike-grid>
```

{% endcode %}

## DikeGrid Pagination API

The DikeGrid internally uses the **MatPaginator** component. Therefore, the DikeGrid definition **wraps** the MatPaginator instance.

Before accessing and using the **pagination API**, you must retrieve the related DikeGrid instance by querying the component's view.

{% tabs %}
{% tab title="row-pagination.component.html" %}

```markup
<dike-grid id="grid-row-pagination" height="600px" #grid="dkgGrid">
</dike-grid>
```

{% endtab %}

{% tab title="row-pagination.component.ts" %}

```typescript
@Component({
  selector: 'row-pagination',
  templateUrl: './row-pagination.component.html',
  styleUrls: ['./row-pagination.component.scss'],

  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RowPaginationComponent implements OnInit, OnDestroy {
  // Retrieve the DikeGridComponent<T> instance from the view:
  @ViewChild('grid') dikeGrid: DikeGridComponent<Employee>;
  // ...
}
```

{% endtab %}
{% endtabs %}

{% hint style="success" %}
The **pagination API** is an instance of type [<mark style="color:green;">`DikeGridPagination`</mark>](https://docs.dikesoft.com/reference/dkgrid-api/dkgridpagination) under the <mark style="color:green;">`DikeGridComponent`</mark> through the **read-only** property named <mark style="color:orange;">`pagination`</mark>.
{% endhint %}

The DikeGrid **pagination API** exposes the **MatPaginator** instance through a **read-only** property named **matPaginator**.

{% hint style="info" %}
Retrieving and using the DikeGrid **pagination API** helps in defining a **custom Datasource**.
{% endhint %}

## Pagination events

Every time the user navigates through the available pages, the DikeGrid instance emits a page event.

Since you can allow the pagination at runtime, the pagination API exposes an event that fires the MatPaginator instance.

The **pagination API** exposes the following events:

| Event             | Description                                                                   |
| ----------------- | ----------------------------------------------------------------------------- |
| `pageChange`      | This event fires every time the user navigates through the available pages.   |
| `paginatorChange` | This event fires when the user allows or disallows the DikeGrid's pagination. |

We are listening to both events:

{% tabs %}
{% tab title="row-pagination.component.html" %}

```markup
<dike-grid id="grid-row-pagination" height="600px" #grid="dkgGrid"
    (pageChange)="onPageChange($event)"
    (paginatorChange)="onPaginatorChange($event)">
</dike-grid>
```

{% endtab %}

{% tab title="row-pagination.component.ts" %}

```typescript
onPageChange(page: PageEvent): void {
  console.log('Page change: ', page);
}

onPaginatorChange(paginator: MatPaginator): void {
  console.log('Paginator change: ', paginator);
}
  
```

{% endtab %}
{% endtabs %}

For the `pageChange` event, the emitted object is the same as the **MatPaginator** sends. For further details, see the [Angular Material official](https://material.angular.io/components/paginator/api) docs.

{% hint style="success" %}
You can also listen to the pagination events from the [<mark style="color:green;">`DikeGridPagination`</mark>](https://docs.dikesoft.com/reference/dkgrid-api/dkgridpagination#events) instance.
{% endhint %}

{% hint style="success" %}
Open the **dev console** to see the output of the pagination events.
{% endhint %}

## Summary

To perform the pagination operation, you must allow it by providing an input property named <mark style="color:orange;">`allowPagination`</mark>. The DikeGrid definition internally uses the **MatPaginator** component. Therefore, every DikeGrid instance exposes its corresponding MatPaginator instance through the **pagination API**.

### Complete code for this section

{% tabs %}
{% tab title="row-pagination.component.html" %}

```markup
<dike-grid id="grid-row-pagination" height="600px" #grid="dkgGrid"
    [displayRowId]="gridProperties.displayRowId"
    [gridElevation]="gridProperties.matElevation"
    [gridElevationValue]="gridProperties.elevationValue"
    [striped]="gridProperties.stripeRows"
    [verticalRowLines]="gridProperties.verticalRowLines"
    
    (pageChange)="onPageChange($event)"
    (paginatorChange)="onPaginatorChange($event)"
    
    [allowPagination]="gridProperties.allowPagination"
    [pageSize]="gridProperties.pageSize"
    [paginationRowHeight]="gridProperties.paginationHeight"
    
    [datasource]="dkgDataSource">
    
    <dike-grid-column
        fieldName="employeeId"
        headerText="Employee Id"
        dataType="Text"
        width="350">
    </dike-grid-column>

    <dike-grid-column
        fieldName="country"
        headerText="Country"
        dataType="Text"
        width="250">
    </dike-grid-column>

    <dike-grid-column
        fieldName="completeNameGroup"
        headerText="Complete Name">

        <dike-grid-column
            fieldName="firstName"
            headerText="Name"
            dataType="Text"
            width="150">
        </dike-grid-column>

        <dike-grid-column
            fieldName="lastName"
            headerText="Surname"
            dataType="Text"
            width="150">
        </dike-grid-column>

    </dike-grid-column>

    <dike-grid-column
        fieldName="gender"
        headerText="Gender"
        dataType="Binary"
        width="130">
    </dike-grid-column>

    <dike-grid-column
        fieldName="age"
        headerText="Age"
        dataType="Numeric"
        contentAlign="center"
        width="100">
    </dike-grid-column>

    <dike-grid-column
        fieldName="email"
        headerText="Email"
        dataType="Text"
        width="300">
    </dike-grid-column>

    <dike-grid-column
        fieldName="hireDate"
        headerText="Hire Date"
        dataType="Date"
        width="150">
    </dike-grid-column>

</dike-grid>
```

{% endtab %}

{% tab title="row-pagination.component.ts" %}

```typescript
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import { MatPaginator, PageEvent } from '@angular/material/paginator';

import { Subscription } from 'rxjs';

import { DikeGridComponent, DikeGridDataSourceInput } from '@dikesoft/angular-data-grid';
import { DikeGridProperties } from 'app/core/config/dike-grid.properties';
import { Employee } from 'app/mock-api/common/employees/data.model';

import { DikeGridConfig } from 'app/services/dike-grid.config.service';
import { SampleData } from 'app/services/sample-data.service';

@Component({
  selector: 'row-pagination',
  templateUrl: './row-pagination.component.html',
  styleUrls: ['./row-pagination.component.scss'],

  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RowPaginationComponent implements OnInit, OnDestroy {
  // Retrieve the DikeGridComponent<T> instance from the view:
  @ViewChild('grid') dikeGrid: DikeGridComponent<Employee>;

  dkgDataSource: DikeGridDataSourceInput<Employee>;
  gridProperties: DikeGridProperties;

  private changeGridPropertiesSubscription: Subscription = Subscription.EMPTY;

  constructor(
    private cdr: ChangeDetectorRef,
    private gridConfig: DikeGridConfig,
    private sampleData: SampleData) { }

  ngOnInit(): void {
    this.dkgDataSource = this.sampleData.getEmployees(10000);
    // Listening to any config property change:
    this.setChangeGridPropertiesSubscription();
  }

  ngOnDestroy(): void {
    this.changeGridPropertiesSubscription.unsubscribe();
  }

  onPageChange(page: PageEvent): void {
    console.log('Page change: ', page);
  }
  
  onPaginatorChange(paginator: MatPaginator): void {
    console.log('Paginator change: ', paginator);
  }

  private setChangeGridPropertiesSubscription(): void {
    this.changeGridPropertiesSubscription.unsubscribe();
    this.changeGridPropertiesSubscription = this.gridConfig.configChange.subscribe((props: DikeGridProperties) => {
      this.gridProperties = props;
      this.cdr.markForCheck();
    });
  }
}

```

{% endtab %}
{% endtabs %}
