While DynamoDB will Paginate your data in forward direction only, You'll have to deal with the Backward Pagination in Front End.
For large Tables (exceeding 1MB size), what DynamoDB does:
- Scans or Queries upto 1MB.
- Returns
LastEvaluatedKey to fetch the next set of data or the Next Page. This value is used as Pagination Key in Front End to paginate back and forth.
LastEvaluatedKey holds the value of the last object fetched from DynamoDB during a Scan or Query.
What you need to do (in Back End):
- Use
LIMIT property of DynamoDB Query to specify you want only 20 items.
- Use
ExclusiveStartKey property of DynamoDB Query to specify that next set of data would start from the specified value of this property.
What you need to do (in Front End):
Keep an array of objects arr[] to host Pagination Keys.
Keep a variable page initialized to -1, whose value will indicate the current page user is on.
- Load the initial page of list into the UI. Now alongside the data, if you have
LastEvaluatedKey, push it into the arr and increment page.
Now, you have a single page and page indicates you're on
Page 0 and arr contains Page Key of next page.
Code of Next Button should follow the logic:
Request your server to fetch the next page using ExclusiveStartKey = arr[page]
When results of next page arrives, you'll again have another LastEvaluatedKey, so again push it into arr and increment page. So you get the picture here, how we save the Page Keys.
Code of Back Button should follow the logic:
Since page variable indicates the Current Page so page - 1 would indicate the previous page. So:
if (page-1>=0) Request your server to fetch the next page using ExclusiveStartKey = arr[page - 1]
You'll have to manage when Back & Next Buttons are available for clicking by using arr[] and page variables after each page is fetched.