Powerapps - Using a Toggle Switch with Integer Data

Viewed 734

I'm VERY new to Powerapps.

My data has integer values that are being used for 0 or 1 (no other values.)

The toggle switch wants a binary value.

How can i convert the reading and writing to binary to be able to work with this data type? Looking for some type of convert function.

2 Answers

You need to manipulate the default value of the toggle control, and you need to ensure that the number control gets set to the correct value when the toggle control is changed in the form.

If you work with list items in a SharePoint custom form, for example, you can add a toggle control to the card for the SharePoint field. Let's call this "FirstToggle". Then set the default of that toggle control to a formula like

ThisItem.myIntegerField = 1

That will return TRUE or FALSE and swing the toggle.

Next, you need to write the state of the toggle back into the number control. The default value for the number control is then

If(FirstToggle.Value,1,0)

If you use a different scenario, different data source, a gallery, etc., the principle is the same: Load the default of the toggle from the value of the stored data with the formula that compares the value with 1. Set the default of the data field by evaluating the state of the toggle.

enter image description here

It depends on whether the Toggle control is inside or outside the Gallery.

Here is an example of outside:

  1. Set the Toggle control's Default property to:

(Long-form code shown)

If(
    glrSampleData.Selected.dataValue = 0, false,
    glrSampleData.Selected.dataValue = 1, true
)

There are some clean up items you'd want to enable here as well.

Example:

  • What happens when the Gallery is first initialized?
  • What state should the Toggle be in?
    • Since there isn't a third logical state for this control, I've found it best to just hide it from the user.
    • In this case, the Toggle value will default to false so you'll want to prevent any data collection from being submitted, etc. Unless the value has been explicitly set by the user.

To hide the Toggle control if the Gallery has no selected record:

  1. Set the OnStart property of the App control to:
Set(varResetGallery, {});
Set(varResetGallery, Blank());
Set(varResetGallery, {})
  1. Set the Visible property of the Toggle control to:

(Again, long-form code shown)

If(
    IsBlank(glrSampleData.Selected), 
    false, 
    true
)

Example in action: enter image description here

Related