Background
I am totally new to PowerApps and found-out that there is no, out-of-the-box (OOB) pagination feature in Datatable or Gallery Control.
I’ve seen a few how-to blogs which walk me through on how to
implement it. Refer below for the list.
But it does not really explain the logic on how it was
constructed.
Thus, this particular blog is all about. We will break-down the
functions used and explain the logic behind it.
What is Pagination?
In layman’s term, it is a process of displaying a chunk/small of results, from a bigger set of results, and PowerApps has no OOB feature of this.
To create a custom pagination in PowerApps, we will use the
following PowerApps functions.
- FirstN – used to display the first set of data, N denotes the number of records to be returned per set.
- LastN – used to display the last set of data, N denotes the number of records to be returned per set.
Both of these functions will return collection which will be used to display records in the datatable or gallery control.
The Logic
FirstN will return a big set of records starting from the 1st record, and LastN will trim the records that we are not interested.”
It’s confusing isn’t? It is easier if there is an illustration.
Let’s start with Page 1 – FirstN will return records 1 & 2 and LastN will return 1 & 2.
Page 2 – FirstN will return 4 records (1-4) but LastN will only return records 3 &4. (Did you now get the picture?)
One more, Page 3 – FirstN will return 6 records, but LastN will only return records 5 & 6.
The Code
The code below is used in Gallery or DataTable Items’ property, where it defines what records will be loaded in that control.
LastN(
FirstN(varRecords, varIncrementalPageItems),
varPagingItems
)
|
Let’s discuss the variables used in the code below and associate them in The Logic above.
<![if !supportLists]>· <![endif]>varRecords – is a collection, which holds all our data from a datasource. This is the datatable records 1-6 in the table above.
<![if !supportLists]>· <![endif]>varIncrementalPageItems – is a counter for FirstN return. This will determine the total number of records, starting from the 1st record. This is indicated by 1st
<![if !supportLists]>· <![endif]>varPagingItems – This defines the number of record to be displayed, per page.
Previous and Next Button Logic
Pagination is incomplete if there is no Previous or Next button.
In PowerApps, majority is event driven. Changes in variable values will take effect to the controls (or code) that is using that variables.
Thus, the Previous and Next button should update varIncrementalPageItems,
Next Button Code
UpdateContext({varCurrentPage:If(varCurrentPage > 0, varCurrentPage + 1, 0)});
UpdateContext({varIncrementalPageItems: varPagingItems * varCurrentPage});
|
varCurrentPage – is the current page number. The next and previous button must increment and decrement it, respectively.
varIncrementalPageItems as define above, is a result by multiplying current varCurrenPage (Page Number) and varPagingItems.
Previous Button Code
UpdateContext({varCurrentPage:If(varCurrentPage > 0, varCurrentPage - 1, 0)});
UpdateContext({varIncrementalPageItems: varPagingItems * varCurrentPage});
|