How to bind user selected from dropdown

I am working on todo app and I want to assign tasks to other users. So there is owner and assignee which is selected from dropdown.

I have dropdown filled with users this way:

self.drop_down_assignee.items = anvil.server.call(‘get_dropdown_users’)

def get_dropdown_users():
return [row[‘name’] for row in app_tables.users.search()]

And I have assignee column in ToDo table which is related to user\s row.

How do I bind the user (dropdown selected value) back to datatables if I have only the name - string and not the user object ?

The dropdown component can accept a list of tuples.

Please see the docs here.

So for example, if you wanted to return the user’s name and row_id, you could do something like the following:

# server
rows=[(r['name'], r.get_id()) for r in app_tables.users.search()]

# client
self.drop_down_1.items=rows

The text that appears in the dropdown list will be the user names, but the returned selected value will be the row id. Pretty nifty.

Hopefully this helps.

1 Like

thank you, this solves my problem.

1 Like

I actually ran into another problem which I can not figure out…I have data binding for the dropdown selected.value

self.item['assignee']['name']

but I think it doesn’t matches the items I am getting from the user tables because even if in the datatables the information is correct, it doesn’t display correctly the selected value in the dropdown.

I tried with self.refresh_data_bindings() and I get this error:

TypeError: ‘NoneType’ does not support indexing
while setting self.drop_down_assignee.selected_value = self.item[‘assignee’][‘name’]
in a data binding for self.drop_down_assignee
at update_binding.py, line 2 column 2

So I tried to insert normal label in the form with the same data binding [‘assignee’][‘name’]` and it displays correctly.

sorry, I was binding the text but not the value, so it’s ok :slight_smile:

Glad it is working. I’m fairly certain that you don’t even have to explicitly set the binding in the IDE. Once you set the drop down’s items to the list of tuples, the binding is all set automatically.