Paging implementation of API response

From ArdorDocs
Jump to: navigation, search
Other languages:

To implement pagination of API responses use the following procedure as implemented in the Ardor wallet.

All API calls which support paging accept the parameters firstIndex and lastIndex.

These parameters are 0 based and inclusive. Internally they are translated into an SQL OFFSET and LIMIT clauses as follows:

   OFFSET = firstIndex 
   LIMIT = lastIndex - firstIndex + 1

To implement paging with page size P, always fetch (P + 1) rows, set the firstIndex parameter to the first row of the page, 0 based, and the lastIndex parameter to (firstIndex + P).

If the returned number of rows is smaller than (P + 1) stop paging. Otherwise remove the last row and continue paging from the next page.

See an example in the NRS.pages.transactions function in Javascript source file nrs.transactions.js under the <Root>/html/www/js folder. The wallet implementation is using two state variables:

   NRS.pageNumber - representing the current page number, 1 based.
   NRS.itemsPerPage - representing the number of rows per table page.

In our example the parameters for the getBlockchainTransactions API call are set as follows:

   "firstIndex": NRS.pageNumber * NRS.itemsPerPage - NRS.itemsPerPage,
   "lastIndex": NRS.pageNumber * NRS.itemsPerPage

To check if we have more pages to load we are setting the NRS.hasMorePages variable like this:

   NRS.hasMorePages = false;
   if (response.transactions.length > NRS.itemsPerPage) {
       NRS.hasMorePages = true;
       response.transactions.pop(); // remove last row in the response array