Search results

Infinite Scrolling in React Grid component

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.

  • In this feature, Grid will not make a new data request when you visit the same page again.
Source
Preview
App.tsx
largeData.tsx
import { ColumnDirective, ColumnsDirective, GridComponent, Inject } from '@syncfusion/ej2-react-grids';
import { PageSettingsModel, InfiniteScroll, InfiniteScrollSettingsModel } from '@syncfusion/ej2-react-grids';
import * as React from 'react';
import { data } from './largeData';

export default class App extends React.Component<{}, {}>{
  public data: object[] = data(1000);
  public pageSettings: PageSettingsModel = { pageSize: 50 };
  public render() {
    return <GridComponent dataSource={this.data} height={300} enableInfiniteScrolling={true} pageSettings={ this.pageSettings }>
            <Inject services={[InfiniteScroll]} />
            <ColumnsDirective>
                <ColumnDirective field='TaskID' headerText='Task ID' width='70' textAlign='Right'/>
                <ColumnDirective field='Engineer' width='100'/>
                <ColumnDirective field='Designation' width='100'/>
                <ColumnDirective field='Estimation' headerText='Estimation' textAlign='Right' width='100'/>
                <ColumnDirective field='Status' width='100'/>
            </ColumnsDirective>
            </GridComponent>
  }
}
const names = ['TOM', 'Hawk', 'Jon', 'Chandler', 'Monica', 'Rachel', 'Phoebe', 'Gunther', 'Ross', 
              'Geller', 'Joey', 'Bing', 'Tribbiani', 'Janice', 'Bong', 'Perk', 'Green', 
              'Ken', 'Adams'];
const hours = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const designation = ['Manager', 'Engineer 1', 'Engineer 2', 'Developer', 'Tester'];
const status = ['Completed', 'Open', 'In Progress', 'Review', 'Testing']
export function data(count: number) {
    const result = [];
    for (let i = 0; i < count; i++) {
        result.push({
          Designation: designation[Math.round(Math.random() * designation.length)] || designation[0],
          Engineer: names[Math.round(Math.random() * names.length)] || names[0],
          Estimation: hours[Math.round(Math.random() * hours.length)] || hours[0],
          Status: status[Math.round(Math.random() * status.length)] || status[0],
          TaskID: i + 1
        });
    }
    return result;
};

InitialBlocks

You can define the initial loading pages count by using infiniteScrollSettings.initialBlocks property. By default, this feature loads three pages in initial rendering.

In the below demo, we have changed this property value to load five page records instead of three.

Source
Preview
App.tsx
largeData.tsx
import { ColumnDirective, ColumnsDirective, GridComponent, Inject } from '@syncfusion/ej2-react-grids';
import { PageSettingsModel, InfiniteScroll, InfiniteScrollSettingsModel } from '@syncfusion/ej2-react-grids';
import * as React from 'react';
import { data } from './largeData';

export default class App extends React.Component<{}, {}>{
  public data: object[] = data(1000);
  public pageSettings: PageSettingsModel = { pageSize: 50 };
  public infiniteOptions: InfiniteScrollSettingsModel = { initialBlocks: 5 };
  public render() {
    return <GridComponent dataSource={this.data} height={300} enableInfiniteScrolling={true} infiniteScrollSettings={ this.infiniteOptions } pageSettings={ this.pageSettings }>
            <Inject services={[InfiniteScroll]} />
            <ColumnsDirective>
                <ColumnDirective field='TaskID' headerText='Task ID' width='70' textAlign='Right'/>
                <ColumnDirective field='Engineer' width='100'/>
                <ColumnDirective field='Designation' width='100'/>
                <ColumnDirective field='Estimation' headerText='Estimation' textAlign='Right' width='100'/>
                <ColumnDirective field='Status' width='100'/>
            </ColumnsDirective>
            </GridComponent>
  }
}
const names = ['TOM', 'Hawk', 'Jon', 'Chandler', 'Monica', 'Rachel', 'Phoebe', 'Gunther', 'Ross', 
              'Geller', 'Joey', 'Bing', 'Tribbiani', 'Janice', 'Bong', 'Perk', 'Green', 
              'Ken', 'Adams'];
const hours = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const designation = ['Manager', 'Engineer 1', 'Engineer 2', 'Developer', 'Tester'];
const status = ['Completed', 'Open', 'In Progress', 'Review', 'Testing']
export function data(count: number) {
    const result = [];
    for (let i = 0; i < count; i++) {
        result.push({
          Designation: designation[Math.round(Math.random() * designation.length)] || designation[0],
          Engineer: names[Math.round(Math.random() * names.length)] || names[0],
          Estimation: hours[Math.round(Math.random() * hours.length)] || hours[0],
          Status: status[Math.round(Math.random() * status.length)] || status[0],
          TaskID: i + 1
        });
    }
    return result;
};

Cache Mode

Cache is used to store the loaded rows object in the Grid instance which can be reused for creating the row elements whenever you scroll to already visited page. Also, this mode maintains row elements based on the infiniteScrollSettings.maxBlocks count value, once this limit exceeds then it will remove row elements from DOM for new rows.

To enable the cache mode in Infinite scrolling, set infiniteScrollSettings.enableCache property as true.

Source
Preview
App.tsx
largeData.tsx
import { ColumnDirective, ColumnsDirective, GridComponent, Inject } from '@syncfusion/ej2-react-grids';
import { PageSettingsModel, InfiniteScroll, InfiniteScrollSettingsModel } from '@syncfusion/ej2-react-grids';
import * as React from 'react';
import { data } from './largeData';

export default class App extends React.Component<{}, {}>{
  public data: object[] = data(1000);
  public pageSettings: PageSettingsModel = { pageSize: 50 };
  public infiniteOptions: InfiniteScrollSettingsModel = { enableCache: true };
  public render() {
    return <GridComponent dataSource={this.data} height={300} enableInfiniteScrolling={true} infiniteScrollSettings={ this.infiniteOptions } pageSettings={ this.pageSettings }>
            <Inject services={[InfiniteScroll]} />
            <ColumnsDirective>
                <ColumnDirective field='TaskID' headerText='Task ID' width='70' textAlign='Right'/>
                <ColumnDirective field='Engineer' width='100'/>
                <ColumnDirective field='Designation' width='100'/>
                <ColumnDirective field='Estimation' headerText='Estimation' textAlign='Right' width='100'/>
                <ColumnDirective field='Status' width='100'/>
            </ColumnsDirective>
            </GridComponent>
  }
}
const names = ['TOM', 'Hawk', 'Jon', 'Chandler', 'Monica', 'Rachel', 'Phoebe', 'Gunther', 'Ross', 
              'Geller', 'Joey', 'Bing', 'Tribbiani', 'Janice', 'Bong', 'Perk', 'Green', 
              'Ken', 'Adams'];
const hours = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const designation = ['Manager', 'Engineer 1', 'Engineer 2', 'Developer', 'Tester'];
const status = ['Completed', 'Open', 'In Progress', 'Review', 'Testing']
export function data(count: number) {
    const result = [];
    for (let i = 0; i < count; i++) {
        result.push({
          Designation: designation[Math.round(Math.random() * designation.length)] || designation[0],
          Engineer: names[Math.round(Math.random() * names.length)] || names[0],
          Estimation: hours[Math.round(Math.random() * hours.length)] || hours[0],
          Status: status[Math.round(Math.random() * status.length)] || status[0],
          TaskID: i + 1
        });
    }
    return result;
};

Limitations for Infinite Scrolling

  • Due to the element height limitation in browsers, the maximum number of records loaded by the grid is limited due to the browser capability.
  • Initial loading rows total height must be greater than the viewport height.
  • Cell selection will not be persisted in cache mode.
  • Infinite scrolling is not compatible with detail template and hierarchy features.
  • Group expand and collapse state will not be persisted in cache mode.
  • The aggregated information and total group items are displayed based on the current view items. To get these information regardless of the view items, refer to the Group with Page topic.
  • Programmatic selection using the selectRows and selectRow method is not supported in infinite scrolling.