Dropdown selected value property returns array format in perspective

Hi,
I am using one multiselect dropdown.
Through scripting I have created one list and binding that list to multiselect dropdown options.
Now I want to check the selected value of multiselect dropdown, So on change script I am trying to get selected value but when I checked by printing, selected value returns below array format value.
image

Here have selected 0th index.
How can I get int 0 value from this, Any idea?

Like this I am binding values to dropdown.

image

Indexing it with [0] should be enough.

You need to treat the value of the dropdown just like you would any other array in python.

For example, I have a Dropdown with with the following structure for options:

[
  {
    "value": 1,
    "label": "One"
  },
  {
    "value": 2,
    "label": "Two"
  },
  {
    "value": 3,
    "label": "Three"
  }
]

A user has selected “One” and “Two”, so props.value is an array which contains 1 and 2 as the values of the selected options.

value: [
    1,
    2
]

To reference those value, you should either directly reference them via index, or loop over the list.

# this script in onChange Script
try:
    system.perspective.print("First selected value: {0}".format(currentValue.value[0]))
    if len(currentvalue.value) > 1:
        # the following attempts at iterating over the selected values should be identical
        # 1
        [system.perspective.print("Selected value: {0}".format(entry)) for entry in currentValue.value]
        # 2
        for entry in currentValue.value:
            system.perspective.print("Selected value: {0}".format(entry))
        # 3
        for i in range(len(currentValue.value)):
            system.perspective.print("Selected value: {0}".format(currentValue.value[i]))
except IndexError:
    system.perspective.print("No values selected")

No, I cant pass hardcode index, It should be dynamic.
I want selected value(of any index) from dropdown so that I can access label of that particular index.

So, let me try to make things clear:
You want a multiple selection dropdown, and what you need to get out of it is the list of LABELS that were selected ? Not the values ? Are the values used in any way ?

I suggest populating your dropdown like this

# Whatever processing takes place before the snippet you showed us and that results in `outputdata`
# If it's the results of processing, you might as well use a pyDataSet so that it's easier to manipulate.
return [
    {
        'value': wcenter,
        'label': wcenter
    } for wcenter in outputdata
]

This is a ROUGH example. You might need to cast to string as you did before, though that could be done earlier in the building of outputdata.
You could also rework the label a bit, maybe something like {'label': wcenter.replace('_', ' ')}, things like that.
Now you can use the label for display only, and use value for your things.

Bind something to dropdown.props.values and you’ll get a list of those wcenter, that you can iterate through like this:

for value in dropdown.prop.values:
    do_something(value)

And you’ll get every wcenter that was selected.

If you really need to have a number associated to your wcenters:

return [
	{
		'label': wcenter,
		'value': {
			'index': i,
			'name': wcenter
		}
	} for i, wcenter in enumerate(outputdata)
]

Now when entries are selected in the dropdown, you’ll get a list that looks like this:

[
  {
    'index': x,
    'name': "center foo"
  },
  {
    'index': y,
    'name': "center bar"
  }
]

That you can iterate through:

for index, name in values.items():
    do_something(index, name)
1 Like

Where did I use a hard-coded index?

I did, she was probably replying to me.

1 Like