Value dropdown selected value

What I’m trying to do:
I want to use the selected value of a dropdown as text.

What I’ve tried and what’s not working:
when I use a command like this

_project = self.projects_dropdown.selected_value

an object is assigned, but not the value.
I’m also missing something like an indec propertie for the dropdow

Code Sample:

# code snippet

Clone link:
share a copy of your app

This should be covered in the introductory tutorials.

As suggested by the forum, could you share a clone of the app so that folks can see more clearly what the issue is?

I suspect that you have data binding to a row object instead of the text contained in that object. I’m just guessing though as you have not quite provided enough information.

https://anvil.works/build#clone:HNJKIQP44Q3ENPHT=M5WETLCM2INZ33KV4JKU7JGI

this is the lonk for cloning the app.

I fill the dropbox items in Timetracker Form Code line 18 using the values of a table.
I want to manipulate the ‘selected value’ e.g. with string manipulation.
I found in the instructions, how to fill the dropdown, but not how to read a specific value

Currently it looks like your drop down is set to a list of tuples. This is sometimes useful since the text that the user sees is associated with an object (or some other data) that the developer needs to use.

If you want the first part (the string) of every tuple associated with the selected value, you would have to search through the drop down items I believe. Or, if the text you want is also contained in the row object, index into that. Either way, I’m not entirely sure I understand exactly what you would like to do, but I am trying.

You can also just set a drop down to a list of strings in which case the selected value is exactly the same as the text shown to the user.

For example,

my_list=['a', 'b', 'c']

self.drop_down.items=my_list

my_value=self.drop_down.selected_value

# returns whichever value is currently selected

I hope that helps somewhat.

I fill the drop via the command:

self.projects_dropdown.items = [(r[“project_select”],r) for r in app_tables.projects.search()]

where ‘project_select’ is a field in table ‘projects’

That works good. The system offers the different values of ‘project_select’ in the dropdown field.
Later in code I want to use the actual selected value of the dropdown and save it in a gloabl variable

def projects_dropdown_change(self, **event_args):
“”“This method is called when an item is selected”""
_globals.tt_project = self.projects_dropdown.selected_value
print (self.projects_dropdown.selected_value)

The print command returns:

<LiveObject: anvil.tables.Row>

but not the value. What is my mistake?

Thanks for your support

By storing the row as the selected value you get access to all the fields in that row. You need to index into the row that gets returned, e.g:

print (self.projects_dropdown.selected_value[“project_select”])
2 Likes

It works very well.
Thanks

Not necessarily a mistake, per se. With

you are asking selected_value to return the corresponding value of r. The dropdown is functioning as a lookup table.

If, instead, you use the simpler
r[“project_select”]
you are asking selected_value to return the value of r[“project_select”] itself. Which seems to be what you were looking for.