- Number of blocks rendered during initial loading
- Efficient data caching and DOM management in grid cache mode
- Limitations
Contact Support
Infinite scroll in Angular TreeGrid component
4 Dec 202415 minutes to read
The infinite scrolling feature in the Tree Grid is a powerful tool for seamlessly handling extensive data sets without compromising tree grid performance. It operates on a “load-on-demand” concept, ensuring that data is fetched only when needed. In the default infinite scrolling mode, a new block of data is loaded each time the scrollbar reaches the end of the vertical scroller. This approach significantly enhances the user experience when working with large data collections in the tree grid.
In this mode, a block of data accumulates every time the scrollbar reaches the end of the scroller. To clarify, in this context, a block represents the pageSize of the tree grid. If the pageSize
is not explicitly specified, the tree grid will automatically calculate it based on the tree grid viewport height and row height.
To enable infinite scrolling, you need to define enableInfiniteScrolling as true and content height by height property.
In this feature, the tree grid will not initiate a new data request when revisiting the same page.
Theheight
property must be specified when enablingenableInfiniteScrolling
.
The following an example that demonstrates how to enable infinite scroll in the tree grid:
import { NgModule,ViewChild } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeGridModule } from '@syncfusion/ej2-angular-treegrid'
import { PageService, InfiniteScrollService } from '@syncfusion/ej2-angular-treegrid'
import { Component, OnInit } from '@angular/core';
import { dataSource, virtualData } from './datasource';
@Component({
imports: [TreeGridModule ],
providers: [PageService, InfiniteScrollService],
standalone: true,
selector: 'app-container',
template: `<ejs-treegrid #treegrid [dataSource]='data' [enableInfiniteScrolling]=true height=317 [pageSettings]='pageSettings' childMapping='Crew' [treeColumnIndex]='1' >
<e-columns>
<e-column field='TaskID' headerText='Player Jersey' width='120' textAlign='Right'></e-column>
<e-column field='FIELD1' headerText='Player Name' width='120'></e-column>
<e-column field='FIELD2' headerText='Year' width='100' textAlign='Right'></e-column>
<e-column field='FIELD3' headerText='Stint' width='120' textAlign='Right'></e-column>
<e-column field='FIELD4' headerText='TMID' width='120' textAlign='Right'></e-column>
</e-columns>
</ejs-treegrid>`
})
export class AppComponent implements OnInit {
public data?: Object[];
public pageSettings?: Object;
ngOnInit(): void {
dataSource();
this.data = virtualData;
this.pageSettings = {pageSize: 30};
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Number of blocks rendered during initial loading
The number of blocks to be initially rendered when the tree grid is loaded. Each block corresponds to a page size of the tree grid, resulting in the rendering of a certain number of row elements determined by multiplying the initial block size with the page size.
You can define the initial loading pages count by using infiniteScrollSettings.initialBlocks property. By default, this property loads three pages during the initial rendering. Subsequently, additional data is buffered and loaded based on either the page size or the number of rows rendered within the provided height.
The following an example of how you can use the initialBlocks
property to set the initial loading pages based on DropDownList input:
import { NgModule,ViewChild } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeGridComponent, TreeGridModule } from '@syncfusion/ej2-angular-treegrid'
import { PageService, InfiniteScrollService } from '@syncfusion/ej2-angular-treegrid'
import { Component, OnInit} from '@angular/core';
import { dataSource, virtualData } from './datasource';
import { ChangeEventArgs, DropDownListModule } from '@syncfusion/ej2-angular-dropdowns'
@Component({
imports: [TreeGridModule,DropDownListModule ],
providers: [PageService, InfiniteScrollService],
standalone: true,
selector: 'app-container',
template: `<div style="display: flex">
<label style="padding: 30px 20px 0 0" > Select initialBlocks count: :</label>
<ejs-dropdownlist #dropdown id='value' style="padding: 26px 0 0 0" #sample index='0' width='220' [dataSource]='dropDownData' (change)='valueChange($event)' ></ejs-dropdownlist>
</div>
<ejs-treegrid #treegrid [dataSource]='data' [enableInfiniteScrolling]=true height=275 [infiniteScrollSettings]='infiniteScrollSettings' [pageSettings]='pageSettings' childMapping='Crew' [treeColumnIndex]='1' >
<e-columns>
<e-column field='TaskID' headerText='Player Jersey' width='120' textAlign='Right'></e-column>
<e-column field='FIELD1' headerText='Player Name' width='120'></e-column>
<e-column field='FIELD2' headerText='Year' width='100' textAlign='Right'></e-column>
<e-column field='FIELD3' headerText='Stint' width='120' textAlign='Right'></e-column>
<e-column field='FIELD4' headerText='TMID' width='120' textAlign='Right'></e-column>
</e-columns>
</ejs-treegrid>`
})
export class AppComponent implements OnInit {
public data?: Object[];
public pageSettings?: Object;
public infiniteScrollSettings?: Object;
@ViewChild('treegrid') public treegrid?: TreeGridComponent;
public dropDownData?: Object[] = [
{ text: 'Select count' },
{ text: '1', value: '1' },
{ text: '2', value: '2' },
{ text: '3', value: '3' },
{ text: '4', value: '4' },
{ text: '5', value: '5' },
{ text: '6', value: '6' },
{ text: '7', value: '7' }
];
ngOnInit(): void {
dataSource();
this.data = virtualData;
this.pageSettings = {pageSize: 30};
this.infiniteScrollSettings = { initialBlocks: 5};
}
valueChange(args: ChangeEventArgs): void {
(this.treegrid as TreeGridComponent).infiniteScrollSettings.initialBlocks = parseInt((args.value as string), 10);
(this.treegrid as TreeGridComponent).refresh();
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Efficient data caching and DOM management in grid cache mode
In Tree Grid cache mode, cached data blocks are reused when revisiting them, reducing the need for frequent data requests while navigating the same block. This mode also manages DOM row elements based on the infiniteScrollSettings.maxBlocks count value. If this limit is exceeded, it removes a block of row elements from the cache to accomodate new rows to the cache.
To enable cache mode, you need to define enableCache property of infiniteScrollSettings as true.
To enable maximum blocks, you need to define maxBlocks
count of infiniteScrollSettings. By default this property value is 3.
The following example demonstrates how to enable/disable cache mode in infinite scrolling of the tree grid based on a Switch component’s change event:
import { NgModule,ViewChild } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeGridComponent, TreeGridModule } from '@syncfusion/ej2-angular-treegrid'
import { PageService, InfiniteScrollService } from '@syncfusion/ej2-angular-treegrid'
import { ChangeEventArgs, SwitchModule} from '@syncfusion/ej2-angular-buttons'
import { Component, OnInit } from '@angular/core';
import { dataSource, virtualData } from './datasource';
@Component({
imports: [TreeGridModule, SwitchModule ],
providers: [PageService, InfiniteScrollService],
standalone: true,
selector: 'app-container',
template: `<div style="padding: 20px 0px 20px 0px">
<label>Enable/Disable Cache mode</label>
<ejs-switch #switch id="switch" [checked]='true' (change)="toggleCacheMode($event)"></ejs-switch>
</div>
<ejs-treegrid #treegrid [dataSource]='data' [enableInfiniteScrolling]=true height=275 [infiniteScrollSettings]='infiniteScrollSettings' [pageSettings]='pageSettings' childMapping='Crew' [treeColumnIndex]='1' >
<e-columns>
<e-column field='TaskID' headerText='Player Jersey' width='120' textAlign='Right'></e-column>
<e-column field='FIELD1' headerText='Player Name' width='120'></e-column>
<e-column field='FIELD2' headerText='Year' width='100' textAlign='Right'></e-column>
<e-column field='FIELD3' headerText='Stint' width='120' textAlign='Right'></e-column>
<e-column field='FIELD4' headerText='TMID' width='120' textAlign='Right'></e-column>
</e-columns>
</ejs-treegrid>`
})
export class AppComponent implements OnInit {
public data?: Object[];
public pageSettings?: Object;
public infiniteScrollSettings?: Object;
@ViewChild('treegrid') public treegrid?: TreeGridComponent;
ngOnInit(): void {
dataSource();
this.data = virtualData;
this.pageSettings = {pageSize: 30};
this.infiniteScrollSettings = { enableCache: true};
}
toggleCacheMode(args:ChangeEventArgs): void {
(this.treegrid as TreeGridComponent).infiniteScrollSettings.enableCache = args.checked;
(this.treegrid as TreeGridComponent).refresh();
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Limitations
- Due to the element height limitation in browsers, the maximum number of records loaded by the tree grid is limited due to the browser capability.
- It is necessary to set a static height for the component or its parent container when using infinite scrolling. The 100% height will work only if the component height is set to 100%, and its parent container has a static height.
- When infinite scrolling is activated, compatibility for copy-paste and drag-and-drop operations is limited to the data items visible in the current viewport of the tree grid.
- Cell selection will not be persisted in cache mode.
- The aggregated information is displayed based on the current view items.
- Programmatic selection using the selectRows and selectRow methods are not supported in infinite scrolling.
- Infinite scrolling is not compatible with the following features:
- Batch editing
- Row / Cell editing
- Row spanning
- Column spanning
- Row template
- Row virtual scrolling
- Detail template
- Autofill
- Limitations of row drag and drop with infinite scrolling
- In cache mode, the tree grid refreshes automatically if the content’s tr element count exceeds the cache limit of the tree grid’s content after the drop action.
- In remote data, changes are applied only in the UI. They will be lost once the tree grid is refreshed. To restore them, you need to update the changes in your database. By using the rowDrop event, you can send the request to the server and apply the changes in your database. After this, you need to refresh the tree grid to show the updated data.
- Infinite scrolling does not support rendering records in a collapsed state. All records must be fully expanded at initial rendering for proper functionality.