Lab - Using the ForAll function in a gallery
Important
For this lab, don't sign in with your credentials. Use the following steps to sign in to your lab environment with the correct credentials:
- Select Sign in to launch VM mode in this unit.
- A PowerShell window and a Windows Command Prompt window appear. After two to three minutes, they close, and Power Apps opens automatically. Wait for the Power Apps home screen.
To see the lab instructions, select the Instructions tab on the lab sidebar.
This hands-on lab shows you how to use the ForAll function in a gallery.
Sign in to Power Apps. (If you're using the virtual environment, skip to step 2.)
From the Home screen left-side navigation panel, select + Create > Blank app > Create.
Name your app ForAll Exercise, leave the format as Tablet, and select Create (skip the popup that might appear on the app canvas).
Select the Insert tab and add a Button control, and set its OnSelect property to this formula:
ClearCollect(CityPopulations, {City:"London", Country:"United Kingdom", Population:8615000}, {City:"Berlin", Country:"Germany", Population:3562000}, {City:"Madrid", Country:"Spain", Population:3165000}, {City:"Rome", Country:"Italy", Population:2874000}, {City:"Paris", Country:"France", Population:2273000}, {City:"Hamburg", Country:"Germany", Population:1760000}, {City:"Barcelona", Country:"Spain", Population:1602000}, {City:"Munich", Country:"Germany", Population:1494000}, {City:"Milan", Country:"Italy", Population:1344000})
If you're using the virtual environment: to copy and paste the text in the virtual environment, select the Commands button on the top left corner of the screen, select Type text, then select Type clipboard text. Paste the copied text in the window.
Select the Button control, and set its Text property to "Collect." Hold the Alt Key and press the Button control. This action creates a collection called CityPopulations and stores all the information in the collection.
Select the + Insert option from the command bar, select a Vertical gallery control, and then choose CityPopulations as the data source.
With the gallery selected, in the properties panel on the right, change the layout to Title, subtitle, and body.
Select the Fields property and change the Body1 field to Population.
With your gallery control selected, ensure that you can edit inside of the top item. Select the + Insert button again, then search for and add a Check box input. Drag it to the right side of the gallery, and set its Text property to "Add".
Insert another button control, set its Text property to "ForAll Collect" and set its OnSelect property to this formula:
Clear(NewCity);ForAll(Gallery1.AllItems,If(Checkbox1.Value= true ,Collect(NewCity,{City:City,Country:Country,Population:Population})))
Insert another Vertical gallery, and choose NewCity as the data source.
On the Properties panel, change the layout to Title, subtitle, and body.
Select the Fields option to change the Body label to Population.
Put your app in Preview mode, check the Add checkbox for several of the cities in your main gallery, and then select the ForAll Collect button. Notice how the new gallery reflects the items that you checked from the original gallery.
You have now used the ForAll command to add all checked items into a collection that displays these items in a new gallery.