How to programmaticaly select dropdown selected index on window/form start

Hello,
I’m not quite getting there syntax-wise: I have a dropdown list component on a form whose data is bound to a SQL query that would populate it with several string items (‘names’) from a table.

When invoking this form at runtime, I need to select one of the items in this list/set based on a known column value (call it ‘OperatorName’) in a different table, and have that selected and shown.

In the scripting, Event Handlers on a property change for the dropdown list, I have been able select the first index with a event.source.setSelectedIndex(0)… But how do I iterate through the items in the list and select the one I need, finding the index for this ‘operator name’, rather than ‘0’, or the first? Thanks much!

Without seeing your code I’m making assumptions but if your string is in the second column of your dataset that your searching and your index is in the first then something like this would return the first index that matches the string your searching for.

ds = dataset_you_want_to_search
p = [x[0] for x in ds if x[1] == 'operator name'][0]
1 Like

Note that @bpreston’s code requires your data is already in the form of a pydataset. Also, a more efficient rewrite would be something like this:

ds = dataset_you_want_to_search
p = next(index for index, operator in ds if operator == 'operator name', None)

Using the next function makes iteration ‘lazy’, meaning that as soon as any match is found, a value is returned; using a square bracket list comprehension requires that the full list be traversed before any value can be returned.

3 Likes

A nice little trick, unpacking a pydataset like that. Except that if I try it, I get an exception for too many values to unpack?

Thanks much, but I’m not showing 2 columns in this particular dataset of the ‘Data’ property of the dropdown (UserName here is coming from the SQL column it is bound to). I’m getting this error, of course there isn’t an index column. But. Event.sourse.setSelectedIndex(0) with a ‘0’ still works…

image001.png

Unpacking like this would require you have exactly two columns in your pydataset. Maybe too fragile to be generally useful, but does make a limited approach like this more readable.

I’m somewhat unclear on exactly what the flow of the logic is supposed to be here.
Right now, every time the data property is updated (by a query?), you’re running this additional query and trying to match up names.
Some questions:
What if there are multiple matches?
Could this be a JOIN operation at the DB level?

The specific error in your screenshot is just syntax; it wants you to change that line to something like this:
p = next((index for index, username in ds if username == row["Operator1"]), 0)

But that will then fail with ‘not enough values to unpack’ because your dataset only has a single column, so try this:
p = next((index for index, username in enumerate(ds) if username == row["Operator1"]), 0)

The enumerate builtin will “add” an index column to the iteration loop (effectively).

1 Like

OK, I got it to work, thanks @PGriffith .

The following gave me an error: ‘Object is not iterable’
p = next((index for index, username in enumerate(ds) if username == row["Operator1"]), 0)

I tried fighting through it, looked that error up in a search, stumbled across the following example, and got it to work:

(I’m using ‘supervisor’ rather than operator in this example. I need to handle both). But this got me going down the right path.

“What if there are multiple matches”: There won’t be , that ‘name’ is a primary key in the table
“Could this be a JOIN”: It’s not, as to the binding on the data property. It is selecting just one column

As to the flow of logic, I had originally got this code / logic handling sample for this some time ago. This for setting the selected index on a dropdown list, when just setting it to the first index (‘0’) rather than saying