Infinite Scroll
Description
Demo
Infinite Scroll with JSON data
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
Last updated