How to: Get the gallery item index in Canvas Apps using Collections
In this #PowerShot I will show you how to get the item index of a gallery item in canvas apps in Power Apps. Currently there is no direct way that supports this scenario so we will use collections and get the index of the current selected item on a gallery control.
Let's Get Started!
We have a SharePoint list data being displayed on a gallery control. We need to show the index of the selected item from the gallery control. Ideally as this is a SharePoint list data source, the ID column could be used for indexing but then, if there were certain filters, sorting etc. applied, the ID indexing cannot be used.
1. On the "OnStart" property of the app, we are creating a collection to get the data from the SharePoint list and adding an index value. Here we are passing the entire data from the data source to the collection and in case, there is any filtering or sorting required, the same can be passed as the first argument of the "ForAll" loop.
2. This is a gallery control on which we are showing the data from the SharePoint list data source.
3. This is a label control to display the index of the selected item from the gallery control. Expression used on the "Text" property of this control:
Expression: LookUp(IndexCollection,Title1 = Gallery1.Selected.Title).Index
Setup in Action
In this post, we saw how to get the index of a selected item on a gallery control using collections in Power Apps.
I hope you found this interesting and it helped you. Thank you for reading!