Assign the selectedValue of a dropdown list after update

Hi,

I am facing a pretty dumb issue. I have a dropdown list. Datas are retrieved from my database [id, text]. This way my IDs are the keys of my list.
I have a button that sends me to another window where i create a new object. When done, I am sent back to the first page, I passed the new id created via window’s parameters. Then when my list execute again the NamedQuery, it will trigger the script.
My script only assign the new id created to the selectedValue of my list.
I made a lot of prints :

  • My list is updating correctly
  • The new id is in the list
  • The assignement works and is cancelled just after.
    So I see the selectedValue changing to my new id value(from -1 to 10), but it goes just after from 10 to -1.
    Is there a method to see why is it happening ? Is there a thing to see what’s changing the value ?

I am very grateful for any help you can give me !

EDIT :
Code that worked :

if event.propertyName == "datas":
    def selectNewValue():
        event.source.selectedValue = event.source.parent.parent.parent.new_id #(My Root Container)
system.util.invokeLater(selectNewValue)

Without seeing what you have, it’s difficult to say with any certainty, but…

Is the dropdown list directly populated by the query? If yes, is it polling? If yes, turn it off.

Hi, thanks for your quick response !
Sadly, it is pretty difficult to show my code as it is on another computer not connected to the internet.
Anyway, my list is directly populated by a NamedQuery and the polling is off.

It sounds like something circular is going on, or things retriggering.

Assuming you’re using the propertyChanged event put something like this in:

print event.propertyName

if event.propertyName == 'selectedValue' and event.newValue != -1:
	do_some_stuff() #Replace with your code. ;)

You can use the console to view what events are being triggered. The If statement is used as a filter so that your script executes only on the property change that you want.

Now that’s strange, the selectedValue ends up as None…
My script was :

if event.propertyName == "datas" :
   event.source.selectedValue = Root Container.id_homologation #(path with event.parent)

I noticed that this is a template using a template using a template and it has to do something with that and the loop has to come from there. I will investigate on this and come back.

Thanks a lot for your responses and making me realize there are a lot of templates used…

1 Like

A technique to consider:

When passing a selected value of some kind into a window/view/template via a parameter, where it will be used to highlight an item in a list/dropdown/table/etc that is populated from a binding, you must defer assignment of the selection until after that list populates. In other words, do NOT bind the selection directly to the parameter, or blindly assign to the selection. Instead, use a propertyChange event to detect when the list/dropdown/table receives its dataset, and then perform your selection assignment.

One caveat in Vision: most lists/tables wipe their selectedRow/selectedIndex properties just before updating their data, then attempt to restore that property afterwards. You almost always want your selection assignment to happen after that restoration, which requires the above propertyChange to defer that assignment with invokeLater.

Thank you very much, invokeLater did handle the thing and now my selectedValue is my last Item inserted.
However, I am confused as the template I am using works fine in another window.
It’s like, if I don’t change window, the template + scripts are working just fine, but if I’m navigating through windows, the properties seems to be wiped out as you mentionned.
Actually, I’ve used this template through 3 windows, and it didn’t work for the 2 that deal with navigation and works for the one where no navigation occurs.
I don’t know if it is my template that handles badly that, or just that the window’s swap make my list to wipe everything out with the old method.

Anyway, I will remember and stick to invokeLater.
Thank you Phil and Jordan for you precious help !

1 Like