cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
EdHansberry
Kudo Collector
Kudo Collector

Using Toggle in a Gallery - and only 1 or 0 can be "ON"

I am building a gallery and want to have a toggle in the gallery for each item. I think I already know that when the toggle is flipped, I'll need to use a PATCH() function to actually update my database. That seems like it will be easy enough.

 

However, I need it to also turn off any other items in the gallery. In other words, only one item can be on. Zero can be on as well if they manually turn the one that is "on" to "off" but if one is on, and they turn another one on, it needs to turn the other one that is on to off.

 

These are quotes, and will usually be 5 or less showing in the gallery, but only one quote can be active. So in the image below, if I turn on the first one, that is fine as they are all off. But if I then turn on the 2nd one, it needs to turn off the first? Make sense?

(don't judge my gallery - still building and it looks horrible right now!)
20191120 16_45_57-Item Development - Saved (Unpublished) - PowerApps and 3 more pages - Work - Mic.png

12 REPLIES 12

@PowerAddict 

 

Awesome, thanks mate! I'm sure I'll get it humming 🙂 

 

Cheers

Thanks @mdevaney. Works perfectly now. I had to make 2 final tweaks:

  1. Set the Default property to: ThisItem.QuoteChosen
  2. I had to wrap the your code with an IF statement so that if it was on, and the user is turning it off, it simply allowed that to happen with no further updates. Your code as written won't let the toggles all be off. There might be a more elegant way to do it, but this works. Many MANY thanks for your help!

 

UpdateContext({varActiveQuoteToggleID: ThisItem.UID});
ClearCollect(
    colQuoteChosenUID,
    ShowColumns(
        galBrowseQuotes.AllItems,
        "UID"
    )
);
If(
    ThisItem.QuoteChosen = true,
    Patch(
        '[dbo].[tblAppItemDevelopmentQuotes]',
        galBrowseQuotes.Selected,
        {QuoteChosen: false}
    ),
    ForAll(
        colQuoteChosenUID,
        Patch(
            '[dbo].[tblAppItemDevelopmentQuotes]',
            LookUp(
                '[dbo].[tblAppItemDevelopmentQuotes]',
                UID = colQuoteChosenUID[@UID]
            ),
            {
                QuoteChosen: If(
                    colQuoteChosenUID[@UID] = varActiveQuoteToggleID,
                    true,
                    false
                )
            }
        )
    )
)

 

 

@PowerAddict 

Update: Ok so I think I got there in the end by simply amending the Default property of the Toggle to:

 

If(Not(ThisItem.IsSelected),false)

 

Thanks anyway!

 

Hi Hardit,

 

Please can you share how to force the unselected toggles to 'off'?! I'm struggling to make the leap on my own from your previous post which just disabled them.

 

Thanks in advance!

Craig

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Users online (1,296)