Infinite Scroll

Description

Infinite scrolling allows the grid to lazy-load rows from the server (or locally) when reaching the scroll bottom (end) position. In its simplest form, the more the user scrolls down, the more rows will get loaded and appended to the in-memory dataset.

Demo

JSON Data - Demo Page / Demo ViewModel

OData Backend Service - Demo Page / Demo ViewModel

GraphQL Backend Service - Demo Page / Demo ViewModel

![WARNING] Pagination Grid Preset (presets.pagination) is not supported with Infinite Scroll

Infinite Scroll with JSON data

As describe above, when used with a local JSON dataset, it will add data to the in-memory dataset whenever we scroll to the bottom until we reach the end of the dataset (if ever).

Code Sample

When used with a local JSON dataset, the Infinite Scroll is a feature that must be implemented by yourself. You implement by subscribing to 1 main event (onScroll) and if you want to reset the data when Sorting then you'll also need to subscribe to the (onSort) event. So the idea is to have simple code in the onScroll event to detect when we reach the scroll end and then use the DataView addItems() to append data to the existing dataset (in-memory) and that's about it.

export class Example {
  scrollEndCalled = false;

  constructor() {
    // we're using a Binding Service but that will most probably be different on your end
    this._bindingEventService = new BindingEventService();
  }

  attached() {
    this.defineGrid();

    // bind any of the grid events
    this._bindingEventService.bind(gridContainerElm, 'onsort', this.handleOnSort.bind(this));
    this._bindingEventService.bind(gridContainerElm, 'onscroll', this.handleOnScroll.bind(this));
  }

  // add onScroll listener which will detect when we reach the scroll end
  // if so, then append items to the dataset
  handleOnScroll(event) {
    const args = event.detail?.args;
    const viewportElm = args.grid.getViewportNode();
    if (
      ['mousewheel', 'scroll'].includes(args.triggeredBy || '')
      && !this.scrollEndCalled
      && viewportElm.scrollTop > 0
      && Math.ceil(viewportElm.offsetHeight + args.scrollTop) >= args.scrollHeight
    ) {
      // onScroll end reached, add more items
      // for demo purposes, we'll mock next subset of data at last id index + 1
      const startIdx = this.sgb.dataView?.getItemCount() || 0;
      const newItems = this.loadData(startIdx, FETCH_SIZE);
      this.sgb.dataView?.addItems(newItems);
      this.scrollEndCalled = false; //
    }
  }

  // do we want to reset the dataset when Sorting?
  // if answering Yes then use the code below
  handleOnSort() {
    if (this.shouldResetOnSort) {
      const newData = this.loadData(0, FETCH_SIZE);
      this.sgb.slickGrid?.scrollTo(0); // scroll back to top to avoid unwanted onScroll end triggered
      this.sgb.dataView?.setItems(newData);
      this.sgb.dataView?.reSort();
    }
  }
}

Infinite Scroll with Backend Services

As describe above, when used with the Backend Service API, it will add data to the in-memory dataset whenever we scroll to the bottom. However there is one thing to note that might surprise you which is that even if Pagination is hidden in the UI, but the fact is that behind the scene that is exactly what it uses (mainly the Pagination Service .goToNextPage() to fetch the next set of data).

Code Sample

We'll use the OData Backend Service to demo Infinite Scroll with a Backend Service, however the implementation is similar for any Backend Services. The main difference with the Infinite Scroll implementation is around the onProcess and the callback that we use within (which is the getCustomerCallback in our use case). This callback will receive a data object that include the infiniteScrollBottomHit boolean property, this prop will be true only on the 2nd and more passes which will help us make a distinction between the first page load and any other subset of data to append to our in-memory dataset. With this property in mind, we'll assign the entire dataset on 1st pass with this.sgb.dataset = data.value (when infiniteScrollBottomHit: false) but for any other passes, we'll want to use the DataView addItems() to append data to the existing dataset (in-memory) and that's about it.

Last updated