A
A
alexbog902020-07-31 09:27:25
Angular
alexbog90, 2020-07-31 09:27:25

How to avoid a network request with undefined parameters by default?

The task is to make the angular material paginator work with the material grid list and material cards. By clicking on the paginator buttons, a network request should be sent to the server with the necessary parameters.
Request example: https://www.rijksmuseum.nl/api/en/collection?key=H... ,
where p is the number of pages with the date, ps is the number of data per page.

In view the following code:

<mat-paginator
  [length]='itemsLength'
  [pageIndex]='pageIndex'
  [pageSize]='pageSize'
  [pageSizeOptions]='pageSizeOptions'
  showFirstLastButtons
  (page)='pageEvent = getServerData($event)'>
</mat-paginator>


In .ts:
public getServerData(event?: PageEvent) {
    console.log('page Event: ', event);
    this.networkService.getQuery('Rembrandt', event?.pageIndex, event?.pageSize)
      .subscribe(response => {
        this.dataSource = response[this.artObjects];
        this.itemsLength = response[this.count];
        if (!!event) {
          this.pageIndex = event.pageIndex;
          this.pageSize = event.pageSize;
        }
      });
    return event;
  }


The problem is that during the initial page load (before I clicked the paginator buttons) event = undefined and, accordingly, the request to the server looks like this:
https://www.rijksmuseum.nl/api/en/collection?key=H..
After clicking on the paginator button, a correct request is sent to the server and all subsequent clicks on the paginator are processed correctly . How to make the initial request to the server go with default values?

I tried to do this at the beginning of the method, but this is how the paginator breaks:
if (!event) {
      event.pageIndex = 0;
      event.pageSize = 10;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vadimMalovaniy, 2020-08-07
@vadimMalovaniy

const pageIndex = event?.pageIndex || 0;
const pageSize = event?.pageSize || 10;
this.networkService.getQuery('Rembrandt', pageIndex, pageSize)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question