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!)
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.
Hi everyone
I knows this is an old question, but this code woks for me!!
In the Default property: If(!ThisItem.IsSelected;false)
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
Thanks @mdevaney. Works perfectly now. I had to make 2 final tweaks:
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
)
}
)
)
)
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
@PowerAddict what did you use to create that small video? I'm keen to develop small clips like this for my Org Apps.
Cheers
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."
@Anonymous - thanks for the video. Good videos are always welcome. I've bookmarked to watch later tonight or in the morning.
@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.
WarrenBelz
146,776
Most Valuable Professional
RandyHayes
76,287
Super User 2024 Season 1
Pstork1
66,093
Most Valuable Professional