Infinite scroll blocks with unknown total
experimental
Load fixed-size blocks from a feed with no total and detect the end of data.
Live example
Expected behavior
The scroll area covers loaded rows plus a growth zone. Scrolling near the bottom loads the next block. When a short block or an explicit last-block flag arrives, the row count becomes exact, the growth zone is removed and endOfDataReached fires once.
Source
const api = createGrid({
rows: [],
columns,
rowModel: 'remote',
blockSize: 100,
infiniteGrowthRows: 100,
remoteSource: {
async fetchWindow({ startIndex, endIndex, cursor, signal }) {
const res = await fetch(`/feed?limit=${endIndex - startIndex}&cursor=${cursor ?? ''}`, { signal });
const { rows, next, last } = await res.json();
return { rows, nextCursor: next, isLastBlock: last };
},
},
});
api.on('endOfDataReached', ({ totalCount }) => announce('All rows loaded'));const api = createGrid({
rows: [],
columns,
rowModel: 'remote',
blockSize: 100,
infiniteGrowthRows: 100,
remoteSource: {
async fetchWindow({ startIndex, endIndex, cursor, signal }) {
const res = await fetch(`/feed?limit=${endIndex - startIndex}&cursor=${cursor ?? ''}`, { signal });
const { rows, next, last } = await res.json();
return { rows, nextCursor: next, isLastBlock: last };
},
},
});
api.on('endOfDataReached', ({ totalCount }) => announce('All rows loaded'));const api = createGrid({
rows: [],
columns,
rowModel: 'remote',
blockSize: 100,
infiniteGrowthRows: 100,
remoteSource: {
async fetchWindow({ startIndex, endIndex, cursor, signal }) {
const res = await fetch(`/feed?limit=${endIndex - startIndex}&cursor=${cursor ?? ''}`, { signal });
const { rows, next, last } = await res.json();
return { rows, nextCursor: next, isLastBlock: last };
},
},
});
api.on('endOfDataReached', ({ totalCount }) => announce('All rows loaded'));const api = createGrid({
rows: [],
columns,
rowModel: 'remote',
blockSize: 100,
infiniteGrowthRows: 100,
remoteSource: {
async fetchWindow({ startIndex, endIndex, cursor, signal }) {
const res = await fetch(`/feed?limit=${endIndex - startIndex}&cursor=${cursor ?? ''}`, { signal });
const { rows, next, last } = await res.json();
return { rows, nextCursor: next, isLastBlock: last };
},
},
});
api.on('endOfDataReached', ({ totalCount }) => announce('All rows loaded'));// JavaScript interop module
const api = createGrid({
rows: [],
columns,
rowModel: 'remote',
blockSize: 100,
infiniteGrowthRows: 100,
remoteSource: {
async fetchWindow({ startIndex, endIndex, cursor, signal }) {
const res = await fetch(`/feed?limit=${endIndex - startIndex}&cursor=${cursor ?? ''}`, { signal });
const { rows, next, last } = await res.json();
return { rows, nextCursor: next, isLastBlock: last };
},
},
});
api.on('endOfDataReached', ({ totalCount }) => announce('All rows loaded'));API reference
| Kind | Name | Type | Description |
|---|---|---|---|
| Option | blockSize | — | Aligns every request to blocks of this size. |
| Option | infiniteGrowthRows | — | Rows added to the scroll area while the total is unknown. |
| Property | WindowResult.isLastBlock | — | Marks the final block. |
| Property | WindowRequest.cursor | — | The previous block's nextCursor. |
| Event | endOfDataReached | — | Fires once when the end of data is known. |