In the last article, we learned about the different controls which exist in PowerApps although there are some more controls which we will discuss going forward before jumping into remaining controls let’s discuss here the most important part Collection.
What is a collection?
A collection is used to store data that can be used in your app or we can say a collection is a group of items that are similar. For example, Student data collection where the collection will store student first name, last name and other information about the student.
To see it practically let’s open PowerApps editor and add a new screen and OnStart function create a collection.
Now to create a collection in PowerApps we have “Collection” function in PowerApp and syntax is as follows
Collect(Collectionname, items)
So, Student collection can be defined as follow.
Collect(StudentCollection, {Name:”Rajat”,LastName:”Jaiswal”}, {Name:”Sandeep”,LastName:”Jain”})
So, we have created our first collection. To view this in action add any control you want here we are using Gallery control.
Once you added this control you will find the data properties and we can see our created collection which is “StudentCollection”
Here we can assign the title and subtitle properties of the gallery with the property Name & LastName of our newly StudentCollection. Just Save the application at this point and reload the app again.
You will find the following screen.
We can check our newly created collection by file option as shown below
In this way, we can create a collection. Now, the next question is how to ADD/EDIT/Remove in the collection. So, let’s understand this now.
Add Collection
Just drag-drop textboxes, labels, and a button to add the data to the collection as shown below. On Add, button onSelect write below function
Now, when you run and click on Add button. You can see the Record appears in your gallery as shown in below figure.
Remove Collection
Now, In the next step, we will delete the record from the collection.
The syntax of this is very simple
Remove (CollectionName,item)
Now, to achieve this add a delete icon in the gallery as shown in below figure.
On select of delete icon, we can write Remove(StudentCollection, This.Item)
Here This.Item is selected item of the gallery to which we need to delete from the collection.
Now, What if we want to delete the entire collection? then in such case, we have
Clear Collection
The syntax is very simple Clear(CollectionName)
So in our case, we have to write Clear(StudentCollection)
Now, to Edit a particular item in a collection the first this is select the item in the text box which we want to edit and to achieve this we bind the textbox by Gallery’s selected item as shown in below figure.
Once you have done with this then on Update button we can write Patch collection function as shown below
So the syntax is very simple
Patch(StudentCollection,Gallery1.SelectedItem, {Name:TextInput4.Text,LastName:TextInput5.Text})
so in this way, we can update the specific item (As shown above we updated only selected item from Gallery).
So, In this session, we understood Collection, add an item in the collection, Remove function to remove a specific item from the collection, Clear Collection to delete entire items from the collection and none other than Patch function to edit the collection.
RJ