PowerApps - Custom "Gallery Pagination" Component

Now in PowerApps, we can build custom PowerApps Reusable Components which can be reused in many of our PowerApps applications. This is still in preview mode.
 
PowerApps - Custom
 
We had a custom requirement to have pagination to PowerApps Gallery control. The current Gallery control in PowerApps supports up/down scroll  navigation. But there is no support available for going to the first page or last page or to the next or previous page, like we usually have in general real-world web applications.
 
So, in order to achieve this, I have built a reusable PowerApps component called “Gallery Pagination”.
 
PowerApps - Custom
 
This component can be resized based on our need and can be used in Canvas Apps having Phone or Tablet layout.
 
This is like Version 1.0 of “Gallery Pagination” component. I would possibly come up with new version soon, possibly with  support of Collection object and a “possibility to jump to exact page” feature. Currently there is no support of Collection object in PowerApps Component.
 
The good part is this is an open source component and if needed you may update it as per your need in your PowerApps environment once you set it up. It is available to download at this git location.
 
 
PowerApps - Custom
 
Just download it and use it (with some supporting actions on Gallery and Parent Screen controls) like mentioned in the steps below.
 
The below steps might look like a lot,  but believe me it takes very little time to implement them. 
 
Before you begin, make sure you have access to a valid O365 tenant (trial or licensed). You will need One SharePoint Site and Valid PowerApps license.
 
So, let’s get started!
 
Step 1
 
Create a sample Demo List in any test SharePoint site and add some dummy items. To see the real ‘Gallery Pagination’ Component usage in PowerApps, add at least 15-20 entries.
 
PowerApps - Custom
 
Step 2
 
Open PowerApps https://web.powerapps.com/ and create a new SharePoint connection like explained here.
 
Step 2.1
 
Click Connections > Click “New Connection”
 
PowerApps - Custom
 
Step 2.2
 
Select “Connect directly(cloud-services)” > Click “Create” Button
 
PowerApps - Custom
 
Step 2.3
 
On SharePoint row, click “+” icon to create a new connection
 
PowerApps - Custom
 
Step 2.4
 
A new SharePoint connection is now created
 
PowerApps - Custom
 
Step 3
 
Create a new Canvas app and add a SharePoint connection
 
Step 3.1
 
In “New app”, Select Canvas. It will open a new tab in browser.
 
PowerApps - Custom
 
Step 3.2
 
In new browser tab, select “Phone layout” (If you want, you can use Tablet layout as well).
PowerApps - Custom
 
Step 3.3
 
In new Canvas app, Open Data Sources from left navigation
 
PowerApps - Custom
 
Step 3.4
 
Select SharePoint Connection created in Step 2
 
PowerApps - Custom
 
Step 3.5
 
Select SharePoint Site
 
PowerApps - Custom
 
Step 3.6
 
Select SharePoint List
 
PowerApps - Custom
 
Step 4 - Add and Configure Gallery Control
 
Step 4.1
 
Add Gallery Control to Canvas App
 
PowerApps - Custom
 
Step 4.2
 
Configure Data source and Layout as shown in image
 
 
Step 5
 
Save Canvas App
 
PowerApps - Custom
 
Step 6 - Add “Gallery Pagination” Component to PowerApps environment
 
Step 6.1
 
In Canvas App, Click File > Open > Browse
 
PowerApps - Custom
 
Step 6.2
 
Select “Gallery Pagination.msapp” from Downloads folder (which we downloaded earlier from Git)
 
PowerApps - Custom
 
Step 6.3
 
This will close our canvas app and will open new App where Component will be visible in Components section.
 
PowerApps - Custom
 
Step 6.4
 
Save Component 
PowerApps - Custom
 
Step 6.5
 
You can now see that Component is added successfully in your PowerApps environment. Close this component browser session now.
PowerApps - Custom
 
Step 7
 
Now open canvas app for adding and using component,
 
Step 7.1
 
Open Canvas App in Edit mode
PowerApps - Custom
 
Step 7.2
 
Click “+” icon in left navigation menu and in the Insert panel at the bottom select “Get more components”
 
PowerApps - Custom
 
Step 7.3
 
Select “Gallery Pagination” component and Import from the Import Panel
 
PowerApps - Custom
 
Step 7.4
 
Now “Gallery Pagination” component should be visible under Library components
 
PowerApps - Custom
 
Step 7.5
 
Add the component to Canvas app
 
PowerApps - Custom
 
Step 7.6
 
Adjust it according to screen size so that all “Gallery Pagination” controls appear correctly
 
PowerApps - Custom
 
Step 8 - Configure Canvas App Screen
 
Add the below snippet in OnVisible event of Canvas App screen.
 
MySPCollection is a new collection where records from DemoList will be stored.
 
InTotalItemsCount, InGalleryHeight, InGalleryTemplateHeight and varReset are input variables to Component control.
 
Gallery1 is the name of gallery control added on screen.
 
Important to note here is that Component Paging will be based on Gallery height and Gallery template height. Hence, we need to pass it to Pagination control.
 
ClearCollect(MySPCollection, SortByColumns(DemoList, "Modified", SortOrder.Descending));
Set(InTotalItemsCount,CountRows(MySPCollection));
Set(InGalleryHeight, Gallery1.Height);
Set(InGalleryTemplateHeight, Gallery1.TemplateHeight);
Set(varReset, GUID());
PowerApps - Custom
 
Step 9 - Configure Component Input Parameters
 
Assign variables used in Step 8 to the component as shown in image below.
 
You may also update a few other input parameters if you wish or just let them have the default values.
 
PowerApps - Custom
 
Details of Component input parameters
 
Input Parameter
(* are mandatory)
Input Values / Default Values
Description
InTotalItemsCount (*)
InTotalItemsCount
Total Items Count
InGalleryHeight (*)
InGalleryHeight
Gallery Height
InGalleryTemplateHeight (*)
InGalleryTemplateHeight
Gallery Template Height
ResetComponent (*)
varReset
To initialize component values based on reset event
InFirstPageControlToolTip
“Go To First Page”
Tooltip of First Page Control
InPreviousPageControlToolTip
“Go To Previous Page”
Tooltip of Previous Page Control
InNextPageControlToolTip
“Go To Next Page”
Tooltip of Next Page Control
InLastPageControlToolTip
“Go To Last Page”
Tooltip of Last Page Control
InControlColor
RGBA(0, 18, 107, 1)
Color to set for component controls
 
Step 10 - Configure Gallery Control Based on Component’s Output Parameters
 
Configure Gallery Control Items field with the help of output parameters from “Gallery Pagination” component.
 
Here Gallery Pagination_1' is the “Gallery Pagination” component added on Canvas app screen.
 
If('Gallery Pagination_1'.OutCurrentPage = 'Gallery Pagination_1'.OutTotalPages,LastN(FirstN(MySPCollection,'Gallery Pagination_1'.OutPageCounter),(InTotalItemsCount - ('Gallery Pagination_1'.OutPageCounter - 'Gallery Pagination_1'.OutItemsPerPage))),LastN(FirstN(MySPCollection,'Gallery Pagination_1'.OutPageCounter),'Gallery Pagination_1'.OutItemsPerPage))
 
PowerApps - Custom
 
You don’t have to worry about what each Output parameter is built for, but just for your information here are the details of Component output parameters,
 
Output Parameter
Description
OutPageCounter
Page counter for each page
OutItemsPerPage
Items shown per page
OutCurrentPage
Current Page Number
OutTotalPages
Total Page Number
 
Note
If there are variables in your real Canvas app where you are using the same Input or Output variable names then you can go to the Component and update carefully the input or output variable names to new ones which are used in Component’s OnReset event, Navigation control’s Select event and Page Number's label control text. After this just save and publish the app and import component again in your canvas app. Now you will have to use new Input or Output variables in above steps.
 
Step 11 - Publish Canvas app
 
Now save and publish the canvas app and open it in new browser window to let Screen OnVisible event execute correctly.
 
 
Step 12 - Open Canvas app in browser
 
Step 12.1
 
Go to the Apps view
PowerApps - Custom
 
Step 12.2
 
Open details of canvas app by using the context menu
 
PowerApps - Custom  
 
Step 12.3
 
Copy or click on the Web link field to open the canvas app in browser
 
Step 13 - “Gallery Pagination” Component in action
 
Canvas app results will look like this and now you can move back and forth to view paginated items using “Gallery Pagination” component. Here I have 16 items and based on my Gallery and Gallery Template Height, it shown me 4 pages.
 
Step 13.1
 
On first page,
 
PowerApps - Custom
 
Step 13.2
 
On second page
 
PowerApps - Custom
 
Step 13.3
 
On third page
 
PowerApps - Custom
 
Step 13.4
 
On the last page
 
PowerApps - Custom
 
That’s it! PowerApps - Custom
 
Happy Learning, anywhere! PowerApps - Custom