What is Collection & How to Manage a Collection in PowerApps ?





5.00/5 (1 vote)
What is collection & how to manage it in PowerApps?
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 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 to 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 follows:
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 new 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 the 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 the 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 thing is to 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 the below figure:
Once you are 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.