There is a simple and very effective way to select multiple Records in a Gallery. The TemplateFill property of Galleries used with the tips below gives complete control so both individual and multiple Records within Galleries can be selected and highlighted
Select Multiple Records in a Gallery
The TemplateFill Property
The TemplateFill property of a Gallery can be used to assign a color to highlight Records in a Gallery. This can be useful to match the Gallery to a theme or for branding purposes. TemplateFill can also be used with the IsSelected function or other formulas to highlight one or more selected Records. To highlight the selected Record, assign the TemplateFill property of a Gallery to the following:
TemplateFill: If(ThisItem.IsSelected=true,RGBA(204, 204, 204, 1),White)
This highlights the selected Record in light grey and all the other Records in white. While this meets some of our needs, it has drawbacks. A limitation is the first Record in a Gallery is always selected when new Records are added. Another obvious limitation is that the IsSelected function can only apply to a single Record at a time. There are better alternatives to using the IsSelected function that give much greater flexibility
No Records Selected
To provide that flexibility, we add an additional Column to the Collection. I have called the additional Column MySelection to which every added Record is initially assigned a value of ‘false’. To load a single record into the Collection with the MySelection Column, the formula would be:
OnSelect: Collect(colName,{First_Name:Input_First_Name.Text,Last_Name:Input_Last_Name.Text,MySelection:false});
The MySelection Column is then referenced by the TemplateFill property of the Gallery:
TemplateFill: If(ThisItem.MySelection=true,RGBA(204, 204, 204, 1),White)
Therefore, when a new record is added to the Collection it isn’t immediately highlighted
Select Multiple Records
Obviously a Gallery in which none of the Records are highlighted has limited use! To address this, the OnSelect property of the Gallery is used to toggle the value of MySelection between ‘true’ and ‘false’ and Patches the change to the Collection. This allow us to select multiple Records in a Gallery
OnSelect: Patch(colName,ThisItem,{MySelection:!ThisItem.MySelection})
This solution gives total flexibility so that zero, single or multiple Records can be selected and highlighted. The are many situations where this could be very useful. An example would be changing a field value on multiple Records at the same time, rather than requiring each Record to be changed individually. To do this the MySelection Column would be referenced in place of the IsSelected function
Single Record Selected
If we initially don’t want a record to be selected, but when one is, we want only to highlight one Record in a Gallery at any one time, we can achieve that too. This is also done via adding the formula below to the OnSelect property of the Gallery prior to the existing line we added earlier
OnSelect: Patch(colName,LookUp(colIName,MySelection=true),{MySelection:false});
Lookup is used to find the Records (if any exist) with a MySelection value of ‘true’. Once found, the value of the MySelection field for those Records is then Patched to make the value ‘false’. The OnSelect line we added earlier, then Patches the MySelection value of the Record currently selected to ‘true’, as can be seen in the demo below
Summary
Using an extra Column in the Collection allows one or multiple fields to be selected and highlighted
The additional Column doesn’t even need to be added to the Data Source of the App. It can simply be created when the data is loaded into the Collection and ignored when the data is syncronised back to the Data Source
Further Reading & References
- Hiredgun.tech: Updating Records from within a Gallery
- Hiredgun.tech: Input Boxes, Galleries & Collections
- Hiredgun.tech: Filter Galleries – 3 Great Tips and Tricks
- Power Apps Reference: Patch
- Power Apps Reference: LookUp