Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use infinite-scroll in combination with mat-table and service in Angular 7?

I am having trouble using infinite-scroll within my mat-table. I get the data with the table.service.ts class from the backend. How can I use the Infinite scroll functionality within my spreadsheet so that more and more content is automatically displayed when I scroll down.

Window - Angular 7. I use Material Table.

liste.component.ts

private load() {
    const liste_sub = this.liste_Service.loadListe('001', 0, LIST_SIZE).subscribe(
      liste=> {
        this.dataSource.data = liste;
      },
      (err: HttpErrorResponse) => {
        // onError()-Rumpf
        this.handleHttpError(err);
      });
    this.subscription.add(liste_sub);
  }

liste.service.ts

  public loadListe(from: number, size: number): Observable<Liste[]> {
    if (!this.cache$ || this.from_cashe$ !== from || this.size_cashe$ !== size) {
      this.from_cashe$ = from;
      this.size_cashe$ = size;
      this.cache$ = this.doRequest(from, size).pipe(
        shareReplay(CACHE_SIZE)
      );
    }
    return this.cache$;
  }

  private doRequest(from: number, size: number): Observable<Liste[]> {
    const params  = new HttpParams().append('from', from.toString()).append('size', size.toString());
    const url = this.API_URL.replace(':vertragsnummer', vertragsnummer);
    const httpOptions = { headers, params };
    return this.httpClient.get<Liste[]>(url, httpOptions);
  }

liste.component.html

<table mat-table [dataSource]="dataSource">
....

</table>

I would like to add virtual scrolling into my table. It should load as much contents as the size of the page. If the user is scrolling down it should load and show further contents. In that case the die page is getting longer. The scrollbar should not be shown inside the table. So no additional scrollbar.

like image 478
sebi.schneider Avatar asked Jul 22 '19 09:07

sebi.schneider


People also ask

How do I add infinite scroll?

Method 1: Adding Infinite Scroll with Catch Infinite Scroll You need to click on it to configure the plugin settings. To start with, you need to choose a trigger option for loading articles. You should select the 'Scroll' option to trigger autoload with scrolling.

What is infinite scroll in angular?

Infinite scrolling is used to load a huge amount of data without degrading the Grid performance. This feature works like the lazy loading concept, which means the buffer data is loaded only when the scrollbar reaches the end of the scroller. To enable Infinite scrolling, set enableInfiniteScrolling property as true.

How do you implement infinite scroll in backend?

Setting up backend First create a new Node project. Open up your terminal and create a new project folder. Install the dependencies, we need express module for now. Now we have a back end simulation ready which will keep on giving us data for any page number and any page size request.


1 Answers

I hope this would help you.

Infinite scrolling isnt working well with mat-table, though it can be achieved without using the infinite scroll module or cdk-virtual-scrolling.

To read more about this https://github.com/angular/components/issues/9858

like image 130
Abishek Amstrong Avatar answered Jan 04 '23 12:01

Abishek Amstrong