Checked items into a semi-colon deliminated string

I recently saw a video made by Audrie Gordon (Deep Dive | Friday Functions Series: Checked Items into Semi-Colon Deliminated String) where I noticed a flaw in the logic. The topic did inspire me though to improve the example and create a blog post about it.

There were actually two flaws:

  • A string was used to check if a certain option was included and should be removed. Example: There are (at least) the two options: “lon” and “Melon”. Unchecking “lon” would also have impact on “Melon”.
  • It did not anticipate for all situations, like removing the first option (which is not preceded by a semi-colon) or when only one option was selected and then unchecking it.

What I did

The main changes are:

  • Using a collection with one column to hold the selected fruits.
  • Only do the checking, adding and removing on the collection. After adding or removing, the string was recreated.

I created a PowerApp (an app created with PowerApps) using a SharePoint list as its data source. This list has a column for storing the selected fruits which is called “Fruits”. I connected to a second SharePoint list which holds all the fruits.

The PowerApp has three screens but for this blog post, I only updated the “EditScreen”. A screenshot of that screen is shown to the right. There was some other functionality on that screen but I made it blur to focus on the topic of this blog post.

I reduced the form in size, and added a gallery and a label below it. This gallery was connected to the SharePoint list holding all fruits.

When checking a fruit, the value is added to the collection with the Collect function. The variable used in the field “Fruits” (myFruits) is updated hereafter. Some extra code is added to remove the semi-colon at the end, which is a result of the Concat function.

When unchecking a fruit, the value is removed from the collection with the RemoveIf function. The variable used in the field “Fruits” (myFruits) is updated hereafter.

Below, the most important property settings are given.

Screen > Property “OnVisible”

UpdateContext({myFruits: BrowseGallery1.Selected.Fruits});
Clear(myFruitsCollection);
ForAll(Split(myFruits,";"), Collect(myFruitsCollection, Result))

, where “BrowseGallery1.Selected.Fruits” is the field “Fruits” of the selected item (start screen).

Control “Fruits” > Propery “Default”

myFruits

Gallery > Control “Checkbox” > Property “Default”

If(ThisItem.Title in myFruitsCollection, true, false)

Gallery > Control “Checkbox” > Property “OnCheck”

Collect(myFruitsCollection, ThisItem.Title);
UpdateContext({myFruits: Concat(myFruitsCollection, Value & ";")});
If(Right(myFruits,1)=";", UpdateContext({myFruits: Replace(myFruits, Len(myFruits), 1, "")}))

Gallery > Control “Checkbox” > Property “OnUncheck”

RemoveIf(myFruitsCollection, Value=ThisItem.Title);
UpdateContext({myFruits: Concat(myFruitsCollection, Value & ";")});
If(Right(myFruits,1)=";", UpdateContext({myFruits: Replace(myFruits, Len(myFruits), 1, "")}))

Control “Label” > Property “Text”

"Number in array: " & CountRows(myFruitsCollection)

Leave a Reply

Your email address will not be published. Required fields are marked *