Angular 4 with ngx infinite scroll

梦想的初衷 提交于 2019-12-20 15:26:39

问题


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,
    }

回答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.



来源:https://stackoverflow.com/questions/47587231/angular-4-with-ngx-infinite-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!