Angular 4 with ngx infinite scroll

All we need is an easy explanation of the problem, so here it is.

I am trying to add an infinite scroll with ngx-infinite-scroll in my Angular 4 project.

The array data has about 800 posts which are from API.

Initially, I want to display 20 posts and every single time the page is scrolled, it will display 20 more posts.

Currently, I can see the console log message (scrolled!) whenever I scroll down.

But I can’t figure out how to append 20 posts into the table when it’s scrolled.

This is the codes that I am trying.

onScrollDown function

onScrollDown(){        
  this.dataService.getPosts().subscribe((posts)=>{
      for (let post of posts){
       let data = '<tr><td>'+ post.title +'</td><td>'+ post.geo +'</td><td>'+ post.Telephone +'</td><td>'+ post.category +'</td><td>Detail</td></tr>';
       $('table.feed tbody').append(data);
      }
});

.

This is the component codes.
posts.component.html

    <div *ngIf="posts?.length > 0;else noPosts" class="search-results" infinite-scroll [infiniteScrollDistance]="2" [infiniteScrollUpDistance]="2" [infiniteScrollThrottle]="50" (scrolled)="onScrollDown()" [scrollWindow]="false">
        <table class="responsive-table feed striped">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>State/City</th>
                    <th>Phone</th>
                    <th>Category</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let post of posts | filter:term">
                    <td>{{post.title}}</td>
                    <td>{{post.geo}}</td>
                    <td>{{post.Telephone}}</td>
                    <td>{{post.category}}</td>
                    <td>Detail</td>
                </tr>
            </tbody>
        </table>
    </div>

posts.component.ts

    import { Component, OnInit } from '@angular/core';
    import { DataService } from '../../services/data.service';
    import { FilterPipe } from '../../filter.pipe';
    declare var jquery:any;
    declare var $ :any;

    @Component({
      selector: 'feed',
      templateUrl: './feed.component.html',
      styleUrls: ['./feed.component.css']
    })

    export class FeedComponent implements OnInit {

    term : '';
    posts: Post[];

      constructor(private dataService: DataService) { }

      ngOnInit() {
          this.dataService.getPosts().subscribe((posts)=>{
              this.posts = posts.slice(0,10);
          });
      }

    onScrollDown(){     
         console.log("scrolled!");   
    }

    interface Post{
        id:number, 
        title:string,
        contact:string,
        Address:string,
        Telephone:number,
        Email:string, 
        Website:string, 
        Establishment:string,
        sector:string,
        category:string,
    }

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

first, save your original array like this

 this.dataService.getPosts().subscribe((response)=>{                  
               this.originalPosts = response;
               this.posts = response.slice(0,20);
          });

 onScrollDown(){
   if(this.posts.length < this.originalPosts.length){  
     let len = this.posts.length;

     for(i = len; i <= len+20; i++){
       this.posts.push(this.originalPosts[i]);
     }
   }
 }

just push it on the same array you don’t need to append it to the table directly, angular will manage itself, its too easy when using angular.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply