Skip to main content

Notifications

Community site session details

Community site session details

Session Id : w5+mmkqrCYaRI2Q5WI2Chq
Power Apps - Building Power Apps
Answered

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

Like (0) ShareShare
ReportReport
Posted on 21 Nov 2019 00:50:38 by 386

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

Categories:
  • vsack1123 Profile Picture
    2 on 11 Apr 2023 at 01:01:58
    Re: Using Toggle in a Gallery - and only 1 or 0 can be "ON"

    An alternative approach to the solution you suggested is to leverage the Set function to set the value of myActiveToggle with the ID of the active toggle switch. This allows for removing the patch and refresh statements altogether. To implement this, you can modify the 'Checked' property of each toggle switch by adding the following logic: If(myActiveToggle = ThisItem.ID, true, false). This will check the current toggle switch only if its ID matches myActiveToggle.

  • cescari Profile Picture
    15 on 23 Nov 2022 at 13:56:37
    Re: Using Toggle in a Gallery - and only 1 or 0 can be "ON"

    Hi everyone

     

    I knows this is an old question, but this code woks for me!!

    In the Default property: If(!ThisItem.IsSelected;false)

  • Community Power Platform Member Profile Picture
    on 21 Aug 2020 at 10:27:43
    Re: Using Toggle in a Gallery - and only 1 or 0 can be "ON"

    @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

  • EdHansberry Profile Picture
    386 on 21 Nov 2019 at 17:47:25
    Re: Using Toggle in a Gallery - and only 1 or 0 can be "ON"

    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
     )
     }
     )
     )
    )

     

     

  • Community Power Platform Member Profile Picture
    on 21 Nov 2019 at 06:35:51
    Re: Using Toggle in a Gallery - and only 1 or 0 can be "ON"

    @PowerAddict 

     

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

     

    Cheers

  • PowerAddict Profile Picture
    7,314 Most Valuable Professional on 21 Nov 2019 at 06:23:30
    Re: Using Toggle in a Gallery - and only 1 or 0 can be "ON"

    Hi @Anonymous 

     

    Glad you liked it! Its a very simple GIF creator with some cool features like controlling what part of the screen gets recorded, checking each frame before saving the GIF, deleting any unwanted frames, pausing while recording and so on. 

     

    Here is the link: https://www.screentogif.com/

     

    You can either install it or just run the executable. Either way works great. 

     

    Hopefully it helps you with your work!

     

    Thanks,

    Hardit Bhatia

    https://thepoweraddict.com

  • Community Power Platform Member Profile Picture
    on 21 Nov 2019 at 06:12:59
    Re: Using Toggle in a Gallery - and only 1 or 0 can be "ON"

    @PowerAddict what did you use to create that small video? I'm keen to develop small clips like this for my Org Apps.

     

    Cheers

  • Verified answer
    mdevaney Profile Picture
    29,987 Super User 2025 Season 1 on 21 Nov 2019 at 04:14:45
    Re: Using Toggle in a Gallery - and only 1 or 0 can be "ON"

    @EdHansberry 

    I have tested my solution now.  There were a few syntax errors but the major change you must make is put this code within the OnSelect property of your toggle instead of OnChange.

     

     

    Set(myActiveToggleID,ThisItem.ID);
    ClearCollect(myGalleryIDs,ShowColumns(Gallery1.AllItems,"ID"));
    ForAll(
     myGalleryIDs,
     Patch(
     your_datasource_name,
     LookUp(your_datasource_name,ID=myGalleryIDs[@ID]),
     {toggle_field_name: If(myGalleryIDs[@ID]=myActiveToggleID,true,false)}
     )
    );
    Refresh(your_datasource_name);

     

     

    I think you will understand why using the OnSelect property instead solves the looping problem but if you would like an explanation please let me know.

     

    ---
    Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

  • EdHansberry Profile Picture
    386 on 21 Nov 2019 at 02:20:28
    Re: Using Toggle in a Gallery - and only 1 or 0 can be "ON"

    @Anonymous - thanks for the video. Good videos are always welcome. I've bookmarked to watch later tonight or in the morning.

  • EdHansberry Profile Picture
    386 on 21 Nov 2019 at 02:18:14
    Re: Using Toggle in a Gallery - and only 1 or 0 can be "ON"

    @mdevaney , this almost works. Almost.

     

    Here is my code. I used a context variable because this is good only for this screen and didn't see a need to set one globally, but otherwise it is pretty much your code with my names:

     

     

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

     

     

    So in the database it is working. I have 3 set up right now. When I select one, it does turn the others off. However, visually, they all turn themselves off once the patch operation runs. I set the Default property to ThisItem.QuoteChosen, but that then causes the one that is visually off, to turn on, and the patch gets in a loop! I removed the REFRESH() and that didn't fix it, nor did it cause problems. Not sure that is needed.

    I set the Default property back to false to keep the app from running all night. Any ideas? If I can just get the toggle to show the values after one has been clicked and not trigger infinite OnChange events, I'd be good.

     

    Because they all show as "off" I cannot test the ability to have them all off as of yet.

     

    I'm kinda new to PowerApps. What is the [@UID] doing? I've seen the brackets and @ sign in dropdowns but have never seen documentation on it. Searching for brackets and @ signs in help doesn't return much for me.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,776 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,093 Most Valuable Professional

Leaderboard