cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Advocate V
Advocate V

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
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

News & Announcements

Community Blog

Stay up tp date on the latest blogs and activities in the community News & Announcements.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Community Highlights

Community Highlights

Check out the Power Platform Community Highlights

Top Solution Authors
Top Kudoed Authors
Users online (7,816)