This has its limitations but if you are fetching a table by the primary key which is an incremented integer value, you could use a combination of lower_bound
, upper_bound
, and limit
and do something like this:
// base setup
var pageSize = 10
var curLower = 0
var curUpper = pageSize - 1
// fetch first page
fetchRows(curLower, curUpper, pageSize)
// next btn click
handleNext(curLower, curUpper, pageSize) {
curLower = curUpper + 1
curUpper += pageSize
const res = fetchRows(curLower, curUpper, pageSize)
if(res.rows.length < pageSize) {
curUpper = curLower + res.rows.length - 1
// handle last page
}
}
// prev btn click
handlePrev(curLower, curUpper, pageSize) {
curUpper = curLower - 1
curLower -= pageSize
const res = fetchRows(curLower, curUpper, pageSize)
if(curLower == 0) {
// handle first page
}
}
async fetchRows(lower, upper, pageSize) {
const res = await rpc.get_table_rows({
json: true,
code: <my_contract>,
scope: <my_scope>,
table: <my_table>,
limit: pageSize,
reverse: false,
lower_bound: lower,
upper_bound: upper
})
return res
}
Needs more handling for stuff like res.rows.length < pageSize
in first page, handling of results that aren't first/last page... but you get the point.