In this blog post, I will walk through the steps required to be able to select multiple items on a gallery control in Power Apps. This is helpful for folks who are trying to select multiple items from a gallery control and perform further actions on the selected items.
Problem Statement
As per the current capabilities, we can only select one item in a gallery control or Listbox. How to overcome this limit and be able to select more than one item? How to use multiple selections for further implementation?
Solution
To add the capability we will need a temporary collection which will store details about all the selections. This collection can be used in the formulas to perform operations later.
Step 1: Add a gallery to the screen and add the Items property as per your use case.
Items property reflects the options that will be available to select
Step 2: Configure the OnSelect property of the gallery control
Here, a collection is created which will store temporary data about the selection.
Expression: If(ThisItem.Value in GallerySelectedCollection.Value, Remove(GallerySelectedCollection,ThisItem),Collect(GallerySelectedCollection,ThisItem))
Explanation: GallerySelectedCollection is the name of the temporary collection. There is an If condition, which checks if the selection already exists in the collection. If it exists, then it will remove the item and if it does not exist, it will create a new entry for the selected item.
Note: If you want to clear the selection on page load or some other action, you can use the expression: Clear(GallerySelectedCollection) and it will remove all the selections.
Step 3: Set the Design based on selection
Now we need some styling to identify the selected items. TemplateFill is a property on the Gallery control which can be used to identify background color for each item.
Expression: If(ThisItem.Value in GallerySelectedCollection.Value,RGBA(3, 121, 137, 1),RGBA(0,0,0,0))
Explanation: GallerySelectedCollection is the name of temporary collection. There is an If condition, which checks if the current item exists in the list. If it exists, then it will show selected color (RGBA(3, 121, 137, 1)). If this does not exist, then it will be transparent(RGBA(0,0,0,0)).
Step 4: Visualize selected data on screen
You can use the GallerySelectedCollection collection to fetch the details of selected items. Instead of using GalleryName.Selected.ColumnName, you can now use GallerySelectedCollection.ColumnName and it will return a one column table of all the values. You can iterate over each item as well.
Expression: Concat(GallerySelectedCollection,Value,Char(10))
Explanation: The above expression concatenates all the values inside the collection for Value column. The input is separated by a new line (char(10)).
Note: You can use various data operation functions like Concat, ForAll etc to perform bulk actions on selected items from the gallery (For example, select a few people from the gallery and send them an email, select multiple items and update their status etc.)
Demo
This post is helpful for folks who are trying to setup a configuration to allow multiple selections from a gallery control. The above approach overcomes the limitations of single selection in Gallery and ListBox control. With the collections we can overcome the limit and use it for further operations.
I hope this was useful for you. In case of any questions or suggestions, feel free to reach me out on twitter at @agarwal_ritika
Comments