0

I have a service that fetches data from a target source (not through an API but via scraping) which can change. I want to do pagination so that I return 35 items per page but the target source is 25 items per page. Is there a way where I can convert between these formats so that I keep my pagination even if the target source changes.

1

You will most likely want to buffer results from the external source, which might be advisable anyway when your service will be used by multiple users and you want to avoid scraping the source every time.

| improve this answer | |
  • Thank you, that is a good idea which I didn't consider. I will include this alongside my solution. – Alexander Hunt Nov 30 '19 at 19:03
1

Ok, So; I've managed to figure this out. I'll post the solution here for future reference.

The Math:

recordNumber = pageNumber x itemPerPage
targetPageWithOffset = recordNumber / targetItemPerPage
targetOffset = floor(targetPageWithOffset)
targetPageOffset = targetOffset x targetItemPerPage

for page 3 with 35 itemPerPage when source is 25 ItemPerPage (Page 1 starts from 0)
2 x 35 = 70
70 / 25 = 2.8 (start on page 3 with 0.8 offset)
floor(2.8) = 2
2.8 - 2 = 0.8
0.8 x 25 = 20

= start on target page 3 and offset by 20... collect data until you get to the target amount.

The code:

Example code

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.