cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
pyee
Level: Powered On

Trouble setting Gallery Default item

I'm using a horizontal gallery to simulate a tabbed interface on my app, and it has 3 hardcoded items, such that:

 

Items = 

 [{ID:"General", DisplayName:"General"}, {ID:"D&C",DisplayName:"D&C"}, {ID:"Land", DisplayName:"Land/Midstream"}]

I have display forms below with their visibility property as: 

gal_tab_nav.Selected.Value.ID = "XXX"

 

Therefore, when no selection is made from the gallery, nothing shows up, which is not ideal.

 

I'm having trouble with what to set the Default to so that when the app loads, the General tab is selected. I've tried simply "General", and "[{ID:"General", DisplayName:"General"}]" (with and without square brackets), but I don't know the syntax for an item object when my values are hardcoded.

 

If the declarative syntax is going to be problematic, what would the best imperative syntax be, on say the screen.OnVisible event be?

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Mike8
Level 10

Re: Trouble setting Gallery Default item

Hello Gargoolgala,

Please try to switch the Items of your gallery to:

Table({ID:"General", DisplayName:"General"}, {ID:"D&C",DisplayName:"D&C"}, {ID:"Land", DisplayName:"Land/Midstream"})

Then you can set the default property to:
{ID:"General", DisplayName:"General"} or
First(Table({ID:"General", DisplayName:"General"}, {ID:"D&C",DisplayName:"D&C"}, {ID:"Land", DisplayName:"Land/Midstream"}))

Then you need to also change

gal_tab_nav.Selected.Value.ID = "XXX"

to

gal_tab_nav.Selected.ID = "XXX"



Hope this helps.

18 REPLIES 18
Community Support Team
Community Support Team

Re: Trouble setting Gallery Default item

Hi @pyee,

 

Based on my understandings, the Default property expects a record value, and you could try to use first() or the Last() function on the data source.

Please see below example, the Default would show me the filtered item after I specify the formula.

large.png

 

Regards,

Mona

Community Support Team _ Mona Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
pyee
Level: Powered On

Re: Trouble setting Gallery Default item

Thanks, Mona.

 

I tried using First(gal_tab_nav.AllItems) since I don't have a data source for these to see if it would pick the first item from that list, but it didn't work. It made the first tab unselectable, even though it gave no errors in the formula bar.

 

Edit: I've also tried: LookUp(gal_tab_nav.AllItems, Value.ID = "General") to the same effect. The other items are selectable, but the General tab can be clicked, but then gets automatically unselected. It actually produces the opposite effect of what I'd expect there.

Gargoolgala
Level 8

Re: Trouble setting Gallery Default item

Pyee,

did you get a resolution to this?  I have exactly the same issue.

pyee
Level: Powered On

Re: Trouble setting Gallery Default item

Not yet. I honestly haven't worked on it much - this was mostly a proof of concept project and I got it working "enough". Seems like a good platform though, just need some of the rough edges knocked off.

JC75
Level: Powered On

Re: Trouble setting Gallery Default item

Same issue - also.

 

As there isnt a data source as such, you cant use the methods often mentioned. 

 

The Items source is self created as you have, so how do you specify that one is to be selected as default.

fbxiii
Level: Powered On

Re: Trouble setting Gallery Default item

I used a Blog from Powerapps to build a tabbed screen and your code looks very similar.  With help from the community forum, I have got this problem sorted.

 

1.  The first point made to me was that the table definition should not be enclosed in the Square brackets as that stores it as values.  This also fixed the issues referencing values in the Table.

 

Changing this defaults the screen to the 1st tab after the App is first opened.  This doesn't reset to the 1st tab each subsequent time the screen is opened though.

 

2.  After some messing around trying to get the Default to work on subsequent views, I tried clearing the Collection holding the table values in the OnHidden property of the screen.

 

This worked and presumably the table is then rebuilt every time the screen is opened again and the first tab is always selected.

 

Update:  I found when clicking through from the 2nd tab on this screen to another, the 1st tab was being selected again when the user returned.  In this case I wanted the selection to remain as the second tab.

 

To overcome this, I created a context variable that is set to true when the Back button is used which will then only Clear my Tabs collection when the back button is used.

 

Screen OnVisible (and the button from 2nd tabs OnSelect):  UpdateContext({ exitscreen:false })

 

Screen OnHidden:  If(exitscreen = true,Clear(Tabs))

 

Gargoolgala
Level 8

Re: Trouble setting Gallery Default item

This last method does not work as we are not using a collection.  So if we remove the [ brackets we have no items in the gallery Smiley Sad

 

I really wish there was proper documentation and examples for Powerapps

Mike8
Level 10

Re: Trouble setting Gallery Default item

Hello Gargoolgala,

Please try to switch the Items of your gallery to:

Table({ID:"General", DisplayName:"General"}, {ID:"D&C",DisplayName:"D&C"}, {ID:"Land", DisplayName:"Land/Midstream"})

Then you can set the default property to:
{ID:"General", DisplayName:"General"} or
First(Table({ID:"General", DisplayName:"General"}, {ID:"D&C",DisplayName:"D&C"}, {ID:"Land", DisplayName:"Land/Midstream"}))

Then you need to also change

gal_tab_nav.Selected.Value.ID = "XXX"

to

gal_tab_nav.Selected.ID = "XXX"



Hope this helps.

Gargoolgala
Level 8

Re: Trouble setting Gallery Default item

Mike - thanks for the insights.

 

I have made he adjustments and fixed all the syntax to match etc. so it is working as before - the table option has helped me at least get a default value in without error.

 

My problem still stands that I enter the screen and it will show my default value but once I navigate to another item it will remember this item if I leave the screen and then return it does not return to the default gallery item.  

My gallery default is : {ID:"GeneralAspects",DisplayName:"General"}

 

I did add in the OnVisible of the screen I am entering the GalleryNav.Selected.ID="GeneralAspects" but it still seems to cache the item value.

I then thought to use Reset(GalleryNav) in the OnVisible but it gives an error stating it expects a resetable control as the input - I suspect this is because a gallery does not ahve a Reset control - not sure why this does not exist Smiley Sad

 

It seems like the default value is being ignored or I am making assumptions on how it works - I would think on the control opening it would be set to the default value.

 

Any further asssitance is appreciated.