Hello Friends,
Welcome back with another post on PowerApps. In last post we were discussing about creating pagination in PowerApps. Till now, we have fetched First Record, Last Record, All Records from SharePoint list. We have also created a Welcome screen from where we can navigate to other screens. We have also created blanks screens for Grid View and Table View. You may visit my last post by clicking on below link-
Now we will create Pagination On Gallery. For this we will use the screen "ScreenGridView". Let's start-
- Below is the look of screen-
- Below are the controls used on this screen-
- The controls list is-
- Now click on ScreenGridView and choose "OnVisible". Add below code-
UpdateContext({RecordsPerPage: 0}); UpdateContext( { RecordsPerPage: RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ) } ); ClearCollect( GalleryDataSource, FirstN( DataSource_Temp, RecordsPerPage ) );
- Here we are defining a local variable "RecordsPerPage" and set its value dynamically by dividing the gallery height with gallery's individual template height. The purpose of this is to automatically change the RecordsPerPage value in case, you adjust the gallery height or template height.
- Later on this variable will hold the value of total number of items to fetch from datasource, depending upon page number. For example, for first page if its value is 12 then for page 2 its value will be 12*2 = 24, for page 3 its value will be 12*3 = 36.
- Now get the first these number of records from DataSource_Temp. You remember, we had initialized this collection on AppStart (PowerApps: Pagination Part-1).
- Highlighted code can be skipped.
- Now click on GalleryDailyTasksGrid >> Items property. Add below code-
LastN( FirstN( DataSource_Temp, RecordsPerPage ), RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ) )
- Here, the code is fetch first "RecordsPerPage" number of items then getting only last those number of records which can be displayed in grid.
- This way you had set up the adjustment of items to be displayed in grid. Now our aim is to just change the value of "RecordsPerPage". By changing the its value, the code written on Items property of GalleryDailyTasksGrid will reset the items to be displayed in grid.
- Now we will write code for next icon "IconNextPage". Choose "OnSelect" property and write below code-
If( RecordsPerPage < CountRows(DataSource_Temp), UpdateContext( { RecordsPerPage: If( RecordsPerPage < CountRows(DataSource_Temp), RecordsPerPage + RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ) ) } ) )
- Here, we are first checking if we have already fetched all records or not, if not, then increment the RecordsPerPage with the number of items to be displayed in grid. The moment, value of RecordsPerPage gets change, items in grid also gets change.
- Similar code has to be written on "OnSelect" property of "IconPreviousPage". The code is-
If( RecordsPerPage < 2 * RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ), UpdateContext( { RecordsPerPage: RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ) } ); FirstN( DataSource_Temp, RecordsPerPage ), UpdateContext( { RecordsPerPage: RecordsPerPage - RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ) } ) )
- Here we are first checking if the value of RecordsPerPage is less than the twice on number of records that can be displayed in grid, If Yes, then reset the RecordsPerPage to number of items that can be displayed in grid, otherwise, subtract the same value from RecordsPerPage to get the previous records.
- Highlighted code can be skipped.
- Now we will write code to get the first page records. For this write below code on "OnSelect" property of button "btnFirstPage_2"-
UpdateContext( { RecordsPerPage: RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ) } ); FirstN( DataSource_Temp, RecordsPerPage );
- Reset the RecordsPerPage to number of items that can be displayed in grid.
- Highlighted code can be skipped.
- Now we will write code to get the last page records. For this write below code on "OnSelect" property of button "btnLastPage_2"-
UpdateContext({RecordsPerPage: CountRows(DataSource_Temp)}); LastN( DataSource_Temp, RecordsPerPage );
- Reset the RecordsPerPage to total number of items in collection.
- Highlighted code can be skipped.
- Now, we have to write code to show the current number of page. For this write below code on "Text" property of "lblCurrentPageNo_2"-
RoundUp( RecordsPerPage / RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ), 0 )
- Now write below code on "Text" property of "lblTotalPages_2" to display total number of pages-
RoundUp( CountRows(DataSource_Temp) / RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ), 0 )
- For displaying total number of records, write below code on "Text" property of "lblTotalRecords_2"-
Concatenate("Total Records:- ", Text(CountRows(DataSource_Temp)))
- Now, if we want to show records of specific number of page, we will use "Search Page" button. Write below code on "OnSelect" property of "btnSearchPage_1"-
If( Value(txtSearch_1.Text) <= RoundUp( CountRows(DataSource_Temp) / RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ), 0 ), UpdateContext( { RecordsPerPage: Value(txtSearch_1.Text) * RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ) } ) )
- Here, first we are checking that the page number entered in text box is valid or not. If valid, then we are resetting the RecordsPerPage.
- Now we will search the items by Title. For this, we had "Search Record". Here, we had implemented filter on Title column. add below code on "OnSelect" property of button "btnSearchRecord_1"-
ClearCollect( DataSourceFiltered, Search( DataSource_Temp, txtSearch_1.Text, "Title" ) ); ClearCollect( DataSource_Temp, DataSourceFiltered ); UpdateContext( { RecordsPerPage: RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ) } );
- Here, first we are filtering the records, then assigning these records to the collection, we had used to display records in grid. Then applying pagination on it.
- This way, we can implement pagination on Gallery control in PowerApps.
- The only issue you may find here is that, if we clicked on "Search Record", our original datasource gets overwritten with the filtered records. For this, we had given Reload button on top of screen.
- Write below code on "OnSelect" property of "IconReload"-
Set( FirstRecord, First(DailyTaskSheetForPowerApps) ); Set( LastRecord, First( Sort( DailyTaskSheetForPowerApps, RecordID, Descending ) ) ); Set( MaxIteration, RoundUp( (LastRecord.RecordID - FirstRecord.RecordID) / 500, 0 ) ); ClearCollect( TotalIterations, AddColumns( AddColumns( Filter( HundredChart, Number <= MaxIteration ), "min", (FirstRecord.RecordID - 1 + (Number - 1) * 500) ), "max", (FirstRecord.RecordID + Number * 500) ) ); Clear(DataSource_Temp); ForAll( TotalIterations, Collect( DataSource_Temp, Filter( DailyTaskSheetForPowerApps, RecordID > min && RecordID < max ) ) ); UpdateContext({RecordsPerPage: 0}); UpdateContext( { RecordsPerPage: RoundDown( GalleryDailyTasksGrid.Height / GalleryDailyTasksGrid.TemplateHeight, 0 ) } ); ClearCollect( GalleryDataSource, FirstN( DataSource_Temp, RecordsPerPage ) );
- Highlight code can be skipped.
- In next post, we will create the first major screen where we will implement pagination (Previous/Next) on Data Table.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.
Stay Safe !
Stay Healthy !
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.