Infinite Scroll
Description
Demo
Infinite Scroll with JSON data
<script setup lang="ts">
import { type Column, Filters, Formatters, GridState, OperatorType, SlickgridVue, SlickgridVueInstance } from 'slickgrid-vue';
import { onBeforeMount, type Ref } from 'vue';
const gridOptions = ref<GridOption>();
const columnDefinitions: Ref<Column[]> = ref([]);
const dataset = ref<any[]>([]);
const scrollEndCalled = ref(false);
let vueGrid: SlickgridVueInstance;
onBeforeMount(() => {
defineGrid();
});
function defineGrid() {
}
function vueGridReady(vueGrid: SlickgridVueInstance) {
vueGrid = vueGrid;
}
// add onScroll listener which will detect when we reach the scroll end
// if so, then append items to the dataset
function handleOnScroll(event) {
const args = event.detail?.args;
const viewportElm = args.grid.getViewportNode();
if (
['mousewheel', 'scroll'].includes(args.triggeredBy || '')
&& !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 = vueGrid.dataView?.getItemCount() || 0;
const newItems = loadData(startIdx, FETCH_SIZE);
vueGrid.dataView?.addItems(newItems);
scrollEndCalled = false; //
}
}
// do we want to reset the dataset when Sorting?
// if answering Yes then use the code below
function handleOnSort() {
if (shouldResetOnSort) {
const newData = loadData(0, FETCH_SIZE);
vueGrid.slickGrid?.scrollTo(0); // scroll back to top to avoid unwanted onScroll end triggered
vueGrid.dataView?.setItems(newData);
vueGrid.dataView?.reSort();
}
}
</script>
<template>
<SlickgridVue gridId="grid1"
v-model:columns="columnDefinitions"
v-model:options="gridOptions"
v-model:data="dataset"
@onVueGridCreated="vueGridReady($event.detail)"
onScroll={$event => handleOnScroll($event.$detail.args)}
onSort={$event => handleOnSort())}
/>
</template>Infinite Scroll with Backend Services
Last updated