# Dropdown Selected Label (Value Label)

**URL:** <https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112>\
**Category:** Ignition\
**Tags:** perspective, vision, ignition81\
**Created:** [January 27, 2023, 8:44pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112 "2023-01-27T20:44:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [January 27, 2023, 8:44pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/1 "2023-01-27T20:44:10Z")

</div>

Wondering what would be the best way to retrieve the label associated with a specific dropdown value. I remember in Vision there is are separate "selection" and "selection text" properties. Would be a nice feature to have for this dropdown as well, not sure why it was removed.

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [January 27, 2023, 8:55pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/2 "2023-01-27T20:55:44Z")

</div>

Perhaps Phil's comment will be helpful,

> [@Dropdown component shows values, not labels](https://forum.inductiveautomation.com/t/dropdown-component-shows-values-not-labels/67688/2):
>
> Perspective doesn't use two columns. I recommend you bind that dataset to custom property (to maintain correspondance between IssueId and IssueName), then bind the dropdown's options to the dataset property with a transform that delivers just the names. Or possibly just reverse the column order.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [January 27, 2023, 9:10pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/3 "2023-01-27T21:10:13Z")

</div>

Not quite the same. **dwb** was trying to extract the labels and values from a dataset for use with the dropdown. I'm aware that perspective doesn't use two columns, but it does still use two properties. It's kind of silly to provide two selection properties (value and label) and only return one (value) and not the other (label).

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [January 27, 2023, 9:19pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/4 "2023-01-27T21:19:22Z")

</div>

Maybe this one will guide you?

> [@Get selected text in dropdown component](https://forum.inductiveautomation.com/t/get-selected-text-in-dropdown-component/67612/8):
>
> Another method.. On the dropdown create a custom property called displayvalue. On the dropdown value prop, right click and add a change script. data = self.props.options for row in range(data.getRowCount()): if (data.getValueAt(row, "value") == self.props.value): self.custom.displayvalue = data.getValueAt(row, "label") break That should then update the custom property with the label. Credit goes to the below, there was another script that took that code a step further that may hel…

If you think it's silly, post your idea here, [https://ideas.inductiveautomation.com/](https://ideas.inductiveautomation.com/)

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [January 27, 2023, 9:26pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/5 "2023-01-27T21:26:31Z")

</div>

That's what I was just working on as well. Would also be nice to be able to set/get the selection with just the index. That way I can add a button to move on to the next item in the list fairly easily.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [January 30, 2023, 2:16pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/6 "2023-01-30T14:16:27Z")

</div>

For those looking to do the same thing, I added 3 custom props to the dropdown.

1. `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.
2. `selected_index` - updated from change script
3. `selected_label` - updated from change script

change script:

```auto
def valueChanged(self, previousValue, currentValue, origin, missedEvents):

	val = currentValue.value
	if val is not None:
		count = len(self.props.options)
		for item in range(0, count):
			if self.props.options[item].value == val:
				self.custom.selected_index = item
				self.custom.selected_label = self.props.options[item].label
	else:
		self.custom.selected_index = None
		self.custom.selected_label = None

```

My reason for wanting to do this was to add buttons that would go to the next or previous item in the list. Fairly easy with scripts with the `selected_index` property.

next:

```auto
def runAction(self, event):
	
	current = self.getSibling('Dropdown').custom.selected_index
	maxIndex = len(self.getSibling('Dropdown').props.options)-1
	if current < maxIndex:
		next = current + 1
	else:
		next = 0
	
	self.getSibling('Dropdown').props.value = self.getSibling('Dropdown').props.options[next].value

```

previous:

```auto
def runAction(self, event):

	current = self.getSibling('Dropdown').custom.selected_index
	maxIndex = len(self.getSibling('Dropdown').props.options)-1
	if current > 0:
		next = current - 1
	else:
		next = maxIndex
	
	self.getSibling('Dropdown').props.value = self.getSibling('Dropdown').props.options[next].value

```

The `enabled` prop for each button is bound to the dropdown. If the value is null, the button is disabled.

---

<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:** [April 7, 2023, 9:22pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/7 "2023-04-07T21:22:21Z")

</div>

Another reason I like MS Access combo boxes! Bind to a query, set the number of columns, and which column holds the value you want to use. Simple and easy!

Thanks for this solution, I hope I can use it to filter a table...

---

<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:** [April 7, 2023, 9:42pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/8 "2023-04-07T21:42:08Z")

</div>

> [@amarks](#):
>
> ```auto
> if val is not None:
> count = len(self.props.options)
> for item in range(0, count):
> 
> ```

Is the props.value or props.options bound to your dataset? In preview mode, I get an error that states  
`self.props.options` has no length. And, I am assuming the change script is on the drop down value...

I dropped a label and bound it's text to the `dropdown.props.value` which, when a `label` is selected from the available list (currently the options are bound to a query because binding to the value did not work), the label component displays the correct "index" value as I would expect an Access combo box to do.

The `len` error does not appear to affect anything in the browser tho...except that the `selected_index` and `selected_label` props do not update.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [April 7, 2023, 9:58pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/9 "2023-04-07T21:58:33Z")

</div>

> [@mdemaris](#):
>
> Is the props.value or props.options bound to your dataset?

Neither are bound to a dataset.

> [@mdemaris](#):
>
> And, I am assuming the change script is on the drop down value...

The change script is on the `dropdown.custom.selected_value` prop  
`dropdown.custom.selected_value` is bound directly to `dropdown.props.value`

I don't get any error in preview mode.

[dropdown\_wIndex.zip](https://forum.inductiveautomation.com/uploads/short-url/rXZxfFPax2QS95Q5cHBNcoS03go.zip) (2.6 KB)

---

<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:** [April 11, 2023, 8:27pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/10 "2023-04-11T20:27:16Z")

</div>

I see how your code works. Just realized I don't think I need to loop through the dataset to do what I need.

Thank you, sir!

---

<div class="post-metadata">

**Author:** ![bfeas](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bfeas/32/3783_2.png) [@bfeas](https://forum.inductiveautomation.com/u/bfeas)\
**Post date:** [May 5, 2023, 1:50pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/11 "2023-05-05T13:50:16Z")

</div>

Here is a solution I used:  
For my options I just appended the option number to the front of my value and parse it out in the event  
selectedIndex = int(self.props.value[0:1])  
labelText = self.props.options[selectedIndex].label  
valueText = self.props.value[1:12]  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/2/32d5a3c06578c9aa99f786e6679ca5d4ae6cca03.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/3/3380ca862da92ab2709964ae56b4d1fccd494296.png)

Interesting, I just noticed the selected index is in the pic above (greyed next to the option number)  
Too bad we can't access it. ☹

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [May 5, 2023, 2:29pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/12 "2023-05-05T14:29:57Z")

</div>

Nice solution! So many ways to skin the cat, just as always when it comes to programming.

Does this work with more than 10 options? I suppose you could use 00, 01, 02, etc. Probably safest to use double digit instead of single in the even the list needs to grow. It's easy to get over 10, not as easy to get over 100.

---

<div class="post-metadata">

**Author:** ![Cas](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@Cas](https://forum.inductiveautomation.com/u/Cas)\
**Post date:** [May 14, 2024, 9:11pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/13 "2024-05-14T21:11:07Z")

</div>

Here's another option:

1. Create a Custom Property called "selectedLabel" on the Dropdown
2. In a Change Script on the Props/value property, put this code:

```python
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if self.props.value is None:
		self.custom.selectedLabel = None
	else:
		self.custom.selectedLabel = next(d['label'] for d in self.props.options if d['value'] == self.props.value)

```

---

<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:** [May 14, 2024, 9:52pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/14 "2024-05-14T21:52:03Z")

</div>

Not a bad idea, but that assumes that a value of `None` means that no option is selected, while it's very possible that `None` is instead the value of a selected option.

---

<div class="post-metadata">

**Author:** ![Cas](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@Cas](https://forum.inductiveautomation.com/u/Cas)\
**Post date:** [May 15, 2024, 2:40pm UTC](https://forum.inductiveautomation.com/t/dropdown-selected-label-value-label/69112/15 "2024-05-15T14:40:38Z")

</div>

Agreed. I liked the one line of code to handle the logic, but the `None` business was just error handling for my case because I have the showClearIcon property set to true, and none of my options result in a valid `None` value.
