11

I am showing the data related to the search term. This data shows all the results at once.

What I want to do is show 6 data once and load the remaining on a scroll.

<li *ngFor="let category of categories">
  {{ category.name }}
</li>

How can I show data on scroll?

7
  • 2
    You could use ngx-infinite-scroll : npmjs.com/package/ngx-infinite-scroll. Commented Jun 7, 2018 at 14:53
  • 2
    @ibenjelloun I hate extra-packages :) Commented Jun 7, 2018 at 14:59
  • 1
    It is open source, you can read the code for inspiration then create your own implementation without adding the lib : github.com/orizens/ngx-infinite-scroll Commented Jun 7, 2018 at 15:00
  • @Sanjay don't try to reinvent wheel again, you could end putting extra effort :p Commented Jun 7, 2018 at 15:00
  • 1
    I believe in the same. But productivity matters alot. Sometime using well known third party package, and going though there their code taught me a lot. May I know the reason why you chosen Angular? Commented Jun 7, 2018 at 15:15

2 Answers 2

27

You could listen to the window:scroll events and load data when the scroll reaches the bottom of the page :

  @HostListener("window:scroll", [])
  onScroll(): void {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
      // Load Your Data Here
    }
  }

Here is a running stackblitz.

Sign up to request clarification or add additional context in comments.

3 Comments

The only issue with it is when resizing the page until no scroll bar on the page, the page cannot load the rest of the data.
adding window.scrollY * 1.1 effectively pre-loads pages and gives a slightly nicer experience
Thank you Mathias for your suggestion, how did you test that ? Can you make a fork with your change and explain how I could notice the difference. Thank you.
1

To auto load data using scroll, we can get the element or div scrollHeight and match with window scroll Y axis height and execute. Here is the code below. Hope it helps

results.component.ts

 page: number = 1;

 @HostListener('window:scroll', ['$event'])
  scrollHandler(event: any) {
    var insightsResults = document.getElementsByClassName(
      'insights-results'
    )[0];
    var childInsights = insightsResults?.scrollHeight;
    var windowScroll = window.scrollY;
    if (Math.floor(windowScroll) >= Math.floor(childInsights)) {
        this.loadMore();
    }
  }
  
  
   loadMore(): void {
    this.page++;
  }
  

results.component.html

<ul
      *ngIf="insights && insights.length"
      class="insights-results__card-list"
      (scroll)="scrollHandler($event)">
      <li
        class="insights-results__card"
        *ngFor="let card of insights | slice: 0:pageSize * page">
        <app-insights-card
          [locale]="locale"
          [data]="card"
          variant="vertical"
        ></app-insights-card>
      </li>
    </ul>

 <div
      *ngIf="insights && insights.length > pageSize * page"
      class="insights-results__button-wrapper">
      <button
        class="insights-results__button"
        (click)="loadMore()"
        [disabled]="pageLoading">
        <span *ngIf="!pageLoading">{{ data.seeMoreResultsCtaText }}</span>
        <span *ngIf="pageLoading">{{ data.loadingText }}</span>
      </button>
    </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.