Example: Using Database Data Paging
This examples demonstrates the use of the read statement's offset option and other arguments to implement a data paging mechanism in a web application.
The example implements a data paging mechanism that allows the user to:
-
Navigate through the set of data using First, Previous, Next, and Last buttons.
-
Specify the page size (in the
PAGESIZE
field). The page size is used to calculate the offset.
For simplicity, no user search profiles are included—the data is retrieved using the ID field (the primary key), and ordered by ID in ascending order.
Detail Triggers
The detail trigger of each navigation button sets the value of the
PAGENUM.CONTROL field and calls the lpRetrieve
ProcScript module.
trigger detail ; of button First public web PAGENUM.CONTROL = 0 call lpRetrieve end; detail
trigger detail ; of button Next public web PAGENUM.CONTROL = PAGENUM.CONTROL+1 call lpRetrieve end; detail
trigger detail ; of button Prev public web PAGENUM.CONTROL = PAGENUM.CONTROL -1 call lpRetrieve end; detail
trigger detail ; button Last public web PAGENUM.CONTROL = -1 retrieve/e "<$entname> end; detail
lpRetrieve
The lpRetrieve
module centralizes
the retrieve action for all buttons, and provides extra processing for the last page.
entry lRetrieve variables numeric vTotalRec endvariables ; If the last page is requested, calculate the page number if (PAGENUM.CONTROL = -1) ;Calculate the total number of pages selectdb count(ID) from "<$entname>" to vTotalRec vTotalRec = vTotalRec / PAGESIZE.CONTROL ; Set the page number vTotalRec -= 1 PAGENUM.CONTROL = vTotalRec[trunc] endif ;retrieve the data, thereby executing the read trigger clear/e "<$entname>" retrieve/e "<$entname>" end ;lRetrieve
- If the last page has been requested
(
$vPageNum$ = -1
), … - … use selectdb to count the number of records …
- … and calculate what the actual page number is. This is needed to go to the previous page. Rounding the number ensures that the number is an integer.
- … but can result in a last page that is too high. If this is the case, decrease the page number by 1.
- Retrieve the data, thereby firing the Read trigger of the entity.