# Perspective Dropdown Value and Text Output

**URL:** <https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765>\
**Category:** Maker\
**Tags:** perspective\
**Created:** [November 21, 2022, 5:35pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765 "2022-11-21T17:35:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunter\_Matuse](https://avatars.discourse-cdn.com/v4/letter/h/b9e5f3/32.png) [@Hunter\_Matuse](https://forum.inductiveautomation.com/u/Hunter_Matuse)\
**Post date:** [November 21, 2022, 5:35pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/1 "2022-11-21T17:35:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [November 21, 2022, 5:45pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/2 "2022-11-21T17:45:58Z")

</div>

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.

```python
# 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`.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [May 1, 2024, 8:17pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/3 "2024-05-01T20:17:26Z")

</div>

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.

> [@cmallonee](#):
>
> `"label": selection`

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.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 1, 2024, 8:29pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/4 "2024-05-01T20:29:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [May 1, 2024, 8:39pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/5 "2024-05-01T20:39:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 1, 2024, 8:43pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/6 "2024-05-01T20:43:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [May 1, 2024, 9:03pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/7 "2024-05-01T21:03:48Z")

</div>

> [@mdemaris](#):
>
> given that the `props.options` come from a query binding that provides two columns/fields.

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:

```python
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.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [May 1, 2024, 9:27pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/8 "2024-05-01T21:27:55Z")

</div>

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:

```python
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:

> [@Dropdown Selected Label (Value Label)](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/6):
>
> For those looking to do the same thing, I added 3 custom props to the dropdown. selected\_value - bound directly to the value prop of the dropdown a. Use a value change script to loop through the array of options and update the index and label. selected\_index - updated from change script selected\_label - updated from change script change script: def valueChanged(self, previousValue, currentValue, origin, missedEvents): val = currentValue.value if val is not None: count = len(self.pro…

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.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 1, 2024, 9:34pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/9 "2024-05-01T21:34:05Z")

</div>

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:

```python
# 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?

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [May 1, 2024, 10:07pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/10 "2024-05-01T22:07:46Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/0/20dc64da6eee28b486552024987267b653678bf6.png)

Dropdown options binding, with no transform:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/1/9101ba472e1fa730ea8d222b8f3375c1d3161e96.png)

With suggested transform:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/b/1be1dfcbec1a25209664729570a40ff4873b6f3b.png)

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.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 1, 2024, 10:13pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/11 "2024-05-01T22:13:28Z")

</div>

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](https://docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/logic/lookup).

Maybe something like,

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 1, 2024, 10:15pm UTC](https://forum.inductiveautomation.com/t/perspective-dropdown-value-and-text-output/66765/12 "2024-05-01T22:15:23Z")

</div>

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

```python
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.
