Perspective Dropdown Value and Text Output

I have a Dropdown menu that has to values, an id and the name. I would like the Name to be the selection and when selected that text is passed to a label. Is there a way to get the dropdown to give back a value AND text for other elements on the page to just take the text. I am using the value for other elements on the page.

Ignition 8.1 Maker on Ubuntu 22.04

A Dropdown option can be given any number of keys, but all it cares about for functional purposes is label and value.

If these id and name entries are coming in from a binding, I recommend using a transform in order to help the Dropdown. The following script will make each option of the Dropdown display the "name" of the entry, and that value will also be used as the value of the Dropdown.

# this is the transform script
options = []
for entry in value:
    selection = entry["name"]  # maybe "Name", depending on the binding
    # note that both label and value are the same value because it sounds like you ONLY care about the name
    options.append({"label": selection, "value": selection})
return options

With this in place, you only need to bind the Labels you mentioned to Dropdown.props.value.

Forgive the revival of an old thread, but I am searching for a way to extract the label of the selected value, given that the props.options come from a query binding that provides two columns/fields. In my case, a user name dropdown, that uses the user id (hidden to user) and user name.

I don't know about you, but this appending results in an error: "No viable alternative at input". I figured I might try your code suggestion here and attempt to get the label (user name) from the selected value (user id). But ran into the aforementioned error.

Please post your code - it looks like selection isn't defined. A typo, perhaps?

Well, I just used cmallonee's code in the transform

OK. A transform of what? What are the contents of value?

A dataset, two columns, int and string. The dropdown is query bound, I select a name from the list, I would like to get that name and use it elsewhere.

So, now I figure there is a way to find the label (aka user name) in the dataset by looking for the value. Kind of like this:

def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if currentValue and currentValue.value:
		val = currentValue.value
		if val is not None:
			dataset = self.props.options
			self.custom.selected_index = val
			self.custom.selected_label = dataset[val]['UserName']

I think there may be a python function I could use on this, like find, but I don't recall at the moment.

After a little search, found an example of python dataset looping, and here is the resulting, and working, code, which does, indeed, return the user's selected name:

def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if currentValue and currentValue.value:
		val = currentValue.value
		if val is not None:
			dataset = self.props.options
			pyDs = system.dataset.toPyDataSet(dataset)
			for item in pyDs:
				if val in item:
					val2 = item[1]
			self.custom.selected_index = val
			self.custom.selected_label = val2

EDIT:
I don't think I have any need for the selected_index, though, as this code came from another use case in another thread here:

Which, I think I had a similar question back on that thread, but don't recall what I was working on then and what I did with it, lol.

You shouldn't need to do all that. cmollonee's code is to set up the dropdown so that the values are already in there.

If your query returns two columns, "name" and "id" then you would populate the dropdown options like this:

# this is the transform script
options = []
for entry in value:
    options.append("label": entry["name"] , "value": entry["id"] )
return options

Now, when you select a dropdown option props.value will return the id directly.

Hmmm, have I misunderstood this? Can you show the dropdown data and what you want to extract?

Ok, let's do this.
Initial binding, root.custom.prop:

Dropdown options binding, with no transform:

With suggested transform:

The "ID" you speak of, should be the "UserID", which is the value of the dropdown component selection. The desired return for the label component is the label of the props.options we are looking at. Hope this makes sense.

Given the UserID of the selected item you should be able to create an expression binding on the Label component to retrieve the Dropdown's selected option label using Expression Language's lookup | Ignition User Manual.

Maybe something like,

lookup(
    parent.custom.user_cbo, 
    Dropdown.value, 
    'error', 
    'UserID', 
    'UserName'
)

Can't use subscripts to get values from a dataset. Convert it to a PyDataset if you want to use that syntax.

options = []
for row in system.dataset.toPyDataSet(value):
    options.append({"label": row["UserName"] , "value": row["UserID"]})
return options

The best for performance would be to change your query to alias the two return columns (SELECT UserID as 'value', UserName as 'label') and change the query return format to JSON, though.

2 Likes