Help writing JS code for pagination

Hi All!

First time user of Retool here. I've read some of the top posts about dealing with pagination, so my apologies if this was already covered and I missed it.

I'm working with an API where the page parameter is called in the Header
The API outputs the first 100 results
If the response has a Status == 206[Partial Return] then this means there are more than 100 results to that request.

My JS skills are close to 0. I would usually get this done in python by creating a while loop and counter based on the condition that if Status == 206 then page =+ 1

I'd love to use something like Retool for this and not have to right a custom python script, create a cron job, etc.

Any ideas how to write the JS query to get this done?

Much appreciated!

You might try using cursor based pagination using the page number as a kind of cursor. Setting that up in Retool requires passing a value that indicates to the table whether or not there's more data to load. So, in this case, you're "Next cursor" value would be page + 1 and your "Has next page" value would be Status == 206. Something like the following:

To verify that could work though, can you check the metadata property of your query to see if the status is included there?

Hi @Kabirdas appreciate the response!

Unfortunately, I do not see Status included in the metadata. At least, I think I'm not seeing it..

How did you get that output?

Forget what I just said. Yes, it does.

But my Next cursor query does not seem to work. When I type that in I get

Cannot read properties of undefined (reading 'page')

Got it, there was some guesswork there on my part. Since all you need is the page number, you might actually try not using the Next cursor at all and just passing table.selectedPageIndex directly to your query. Does that work?

Going to need a bit more hand holding on this. Here is what my query looks like:

Where do I add that? In URL parameters?


No problem! Where you add it depends on what your API is expecting. You mentioned earlier that you pass the page as one of the headers, correct? Try setting the key to whatever your API expects and then passing {{ yourTableName.selectedPageIndex }} as the value. (You might need to pass {{ yourTableName.selectedPageIndex + 1 }} depending on if your API is 0-based or 1-based)