Invalid value is popping up on dropdowns

What I’m trying to do:
Problem:
Our screen is input screen, which writes several values as selected using drop-downs (as shown Fig2).

The values in dropdowns are loaded from tables (as shown in Fig1), after loading and inserting the data we are trying to clear the input for next fresh-screen, however we get a in the screen after successful insertion of data. Here we expecting an empty label for client_code.
We do have many dropdowns like this in our application, we would like to clear the inputs after successful insertion of data to external-database.

What I’ve tried and what’s not working:

Code Sample:

# load the dropdown values for client_code
self.client_code_dd.items = [(r['client_code'], r) for r in app_tables.client_names.search()]

# insert data to table (external database)
anvil.server.call(
      'insert_data',
      self.text_box_id.text,
      self.client_code_dd.selected_value["client_code"]
)

# clear inputs
self.client_code_dd.selected_value = None,

# this is a formatted code snippet.
# paste your code between ``` 

Screen-Shots attached:




Clone link:
share a copy of your app

I think that your problem is that you are trying to assign a value that does not exist in the list.
The solution is to add that value to the list, something like this:

# load the dropdown values for client_code
items = [(r['client_code'], r) for r in app_tables.client_names.search()]
items.insert(('<Select a client name>', None), 0)
self.client_code_dd.items = items

# insert data to table (external database)
if self.client_code_dd.selected_value:
    anvil.server.call(
          'insert_data',
          self.text_box_id.text,
          self.client_code_dd.selected_value["client_code"]
    )

# clear inputs - this should work because None is included in the list of values
self.client_code_dd.selected_value = None
2 Likes

It also looks like you have a stray comma on the final line which means you are assigning the selected value to a tuple (None,) rather than the intended None.

Our intention is to see blank in client_code after successful insertion of data in external database. However in the screen client_code is not initialising to blank after insertion.

Step1:
We choose, Id, client_code, asset_type, entry_type, Exit_type and finally submit.

![image|690x98](upload://31cRebQu9bgMq9oHaV6U9w5Fg2I.png)

<after clicking submit, if you notice the screen ID is cleared, Client_code and other fields are not cleared>

In the code we have:
Client_code_dropDown (this is not a problem)
def datadrop_select(self):
“”" This method selects the data-drop down for config-columns"""

self.client_code_dd.items = [(r['client_code'], r) for r in app_tables.client_names.search()]

def submit_click(self, **event_args):
“”“This method is called when the button is clicked”""
# insert data to config table
self.insert_data_config()
alert(f"Data successfully entered into the config table")

# Clear the fields
self.clear_inputs()

def insert_data_config(self):
“”" This methond will insert data to config table"""
anvil.server.call(
‘insert_data’,
self.text_box_id.text,
self.client_code_dd.selected_value[“client_code”]
)

def clear_inputs(self): (Here we are try to clear, to see empty field (for client_code))
“”" This function clears all input values after data insert"""
self.text_box_id.text = ‘’,
self.client_code_dd.selected_value = None,

We are expecting, like text_box_id.text shows a empty after the click-submit. This does not happen for a drop down client_code_dd.selected_value (see below screen shot)

This line cannot work because the list of tuples previously assigned to self.client_code_dd.items does not contain None.

If you want the drop down to show a string, you need to include a tuple with that string.

If you want that string to be an empty, then you need to add a tuple with an empty string in it. You could change the second line in my previous answer to this:
items.insert(('', None), 0)

Setting the selected item to None should also work without including an extra item, so long as the drop down includes a placeholder.

(If there is no None value in the list of items, then setting the selected value to None will set it to the placeholder - if a placeholder was included)

2 Likes