The OpenGridX DataGrid supports infinite scrolling (lazy loading), allowing you to load large datasets incrementally as the user scrolls.
When infinite scroll is enabled, the grid:
- Disables client-side slicing (pagination in the UI).
- Uses
paginationModelinternally to track the current page and page size. - Triggers
onRowsScrollEndwhen the user scrolls near the bottom of the grid, signaling that more data should be fetched. - Data is typically appended to the existing rows, rather than replacing them.
To enable infinite scrolling:
- Set
pagination={false}to disable UI pagination controls. - Set
paginationMode="infinite". - Implement
onRowsScrollEndto update your data fetching logic/state (e.g., increment page number). - Ensure your
dataSourcemanages the loaded rows correctly (usually appending).
import { DataGrid, GridDataSource } from '@opencorestack/opengridx';
// ... setup columns and mockApi ...
export default function InfiniteScrollDemo() {
const [paginationModel, setPaginationModel] = useState({
page: 0,
pageSize: 50
});
const [loading, setLoading] = useState(false);
const [allRows, setAllRows] = useState([]);
const dataSource: GridDataSource = useMemo(() => ({
getRows: async (params) => {
setLoading(true);
try {
// Fetch NEXT batch of rows from server
const response = await mockApi.getRows(params);
// CRITICAL: Append new rows to existing rows
// (Or ensure response.rows contains ONLY new rows and parent handles appending)
return response;
} finally {
setLoading(false);
}
}
}), []);
const handleScrollEnd = useCallback(() => {
if (!loading) {
// Increment page to fetch next batch
setPaginationModel(prev => ({
...prev,
page: prev.page + 1
}));
}
}, [loading]);
return (
<DataGrid
rows={[]} // Rows managed by dataSource
columns={columns}
dataSource={dataSource}
// KEY PROPS FOR INFINITE SCROLL
pagination={false}
paginationMode="infinite"
sortingMode="server"
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
onRowsScrollEnd={handleScrollEnd}
/>
);
}onRowsScrollEnd: Triggered whenscrollHeight - scrollTop - clientHeight < threshold(default 100px).- Request Handling:
useGridDataSourceincludes logic to cancel stale requests if scrolling happens too quickly. - Appending vs Replacing:
- In
infinitemode, rows fetched for new pages are appended. - Rows fetched due to sorting/filtering changes replace the entire dataset (resetting to page 0).
- In