Kamilox
Kamilox

Reputation: 55

Clarity pagination - output from clr-dg-page-size

I'm writing app in angular 8 with clarity.

I use datagrid with pagination. I need fetch data after change items per page but there is not any output from clarity components. There is only output for changing pages.

There is some html:

<clr-dg-footer>
    <clr-dg-pagination #pagination [clrDgPageSize]="itemsPerPage" [clrDgTotalItems]="totalElements" [clrDgLastPage]="totalPages"
    (clrDgPageChange)="onChangePage($event)">
      <clr-dg-page-size [clrPageSizeOptions]="[5,20,50,100]" (change)="onItemsPerPageChange($event)">Samochodów na stronę</clr-dg-page-size>
      {{pagination.firstItem + 1}} - {{pagination.lastItem + 1}}
      z {{pagination.totalItems}} samochodów
    </clr-dg-pagination>
  </clr-dg-footer>

And there is how I'm doing it now:

onItemsPerPageChange(event) {
    let newValue = +event.target.value.slice(3);
    if(this.itemsPerPage !== newValue) {
      this.itemsPerPage = newValue;
      this.fetchCars();
    }
  }

It works but I know it's terrible way to do this.

Do you know how to do it in correct way? I don't have any ideas.

Upvotes: 4

Views: 3719

Answers (1)

zerocewl
zerocewl

Reputation: 12804

I think the common way with Claritys datagrid is to use ClrDatagridStateInterface together with (clrDgRefresh) binding inside the datagrid template.

Have a look at Claritys Server-Driven datagrid API or this clarity-dg-server-driven Stackblitz.

In your component you can make use of ClrDatagridStateInterface

class MyComponent {
    users: User[];
    total: number;
    loading: boolean = true;

    refresh(state: ClrDatagridStateInterface) {
        this.loading = true;
        ...
        this.inventory.filter(filters)
            .sort(<{by: string, reverse: boolean}>state.sort)
            .fetch(state.page.from, state.page.size)
            .then((result: FetchResult) => {
                this.users = result.users;
                this.total = result.length;
                this.loading = false;
            });
    }

And in your template you have to bind (clrDgRefresh) to your refresh method:

<clr-datagrid (clrDgRefresh)="refresh($event)" [clrDgLoading]="loading">
    <clr-dg-column>User ID</clr-dg-column>
    <clr-dg-column [clrDgField]="'name'">Name</clr-dg-column>
    <clr-dg-column [clrDgField]="'creation'">Creation date</clr-dg-column>
    <clr-dg-column [clrDgField]="'pokemon'">Pokemon</clr-dg-column>
    <clr-dg-column [clrDgField]="'color'">Favorite color</clr-dg-column>

    <clr-dg-row *ngFor="let user of users">
        <clr-dg-cell>{{user.id}}</clr-dg-cell>
        <clr-dg-cell>{{user.name}}</clr-dg-cell>
        <clr-dg-cell>{{user.creation | date}}</clr-dg-cell>
        <clr-dg-cell>{{user.pokemon.name}}</clr-dg-cell>
        <clr-dg-cell>
            <span class="color-square" [style.backgroundColor]="user.color"></span>
        </clr-dg-cell>
    </clr-dg-row>

    <clr-dg-footer>
        {{pagination.firstItem + 1}} - {{pagination.lastItem + 1}}
        of {{total}} users
        <clr-dg-pagination #pagination [clrDgTotalItems]="total"></clr-dg-pagination>
    </clr-dg-footer>
</clr-datagrid>

Here you can find the sources from Claritys server-driven example.

Upvotes: 2

Related Questions