I’m having strange problem with the dropdown.
If you start the app, select 3rd option from the repeating panel and click open. A new modal window will appear.
That window contain several areaboxes, dropdowns and textboxes. I did something wrong with the code.
for ex. in dropdown “Matrix” I can see the correct options but the value from the database was not selected (“Serum”) and show only the defined placeholder
I want to append the dropdown based on first datatable and then select the data from the main data table but leave the dropdown to do some changes if needed.
class AssayModal(AssayModalTemplate):
def __init__(self, **properties):
self.init_components(**properties)
#Key for assigning ACN to opened modal from AssayDatabase
self.acn_m.text = self.item['acn_m']
#search for application code in databank
self.item = app_tables.assay_databank.get(application_code = self.acn_m.text)
#dropdown matrix
self.tb_matrix.items = [(r['matrix_dd'], r) for r in app_tables.dropdown.search() if r['matrix_dd'] is not None]
self.tb_matrix.text = self.item['matrix']
self.old_tb_matrix.selected_value = self.item['matrix']
When you fill a repeating panel with a tuple, e.g. (r['matrix_dd'], r), the first item in the tuple is what the user sees, but the second item is what the actual value is. This is what you want. You want the user to see plain text, but you want to be able to get the actual row associated with that text.
The problem comes when you do this: self.old_tb_matrix.selected_value = self.item['matrix']
You must set the selected value to something that would been in the 2nd spot in the tuple, e.g. a row. You’re setting it to a string.
You could solve this in a variety of ways. You could do a search on the table using your string to get the row, and then set the row as the selected item. Or you could search through the items list and find the tuple whose first element is the string, and set the selected value to the second element.
Ok I get it now, why I couldn’t add the searched stirng and followed your instruction to extract the index for my string.
I’ve tried a new approach to find an index in my list of tuples:
[index for index, item in enumerate(tup_list) if item == (searched_str)]
For some reason I was always getting index = 3. I’ve even changed the searched string to different values just to confirm it and even assigned the found touple to a textbox.
After checking found touple in a textbox it was clear to me that the index = 3 is the end of my touple so he found nothing of my searched string and gave me last row back…
Now I know why it’s not working. I’m looking for a substring in the list of touples.
On the picture below the list of tuples. After extracting it my row consist of for ex. ‘Serum’, <anvil.tables.Row: … so the whole row is my string and it is not possible to find only a “Serum” there…
How can I extract a substring from a list of tuples and get the corresponding index back?
#dropdown matrix
tup_list = [(r['matrix_dd'], r) for r in app_tables.dropdown.search() if r['matrix_dd'] is not None]#list of tuples
searched_str = self.item['matrix']#searched string
[index for index, item in enumerate(tup_list) if item == (searched_str)]
self.tb_matrix.items = [(r['matrix_dd'], r) for r in app_tables.dropdown.search() if r['matrix_dd'] is not None]
self.tb_matrix.text = self.item['matrix']
self.old_tb_matrix.selected_value = index
self.lb_created_user.text = index#I use it to check the found index
One more question, how you can use the select_value to set the value in dropdown list to a specific value?
Ok I’ve found a way to extract the tuple that contain the value that should be showed in the drop down and adding the rest to it
create a list of tulps
find the tulp that contain the value that should be showed with index[0] append it to a new list, while doing that append all tulps that don’t match in 2nd List
combine both lists
assign to the dropdown @jshaffstall thanks for your help. Your tips were really helpful
#dropdown matrix
tup_list = [(r['matrix_dd'], r) for r in app_tables.dropdown.search() if r['matrix_dd'] is not None]#list of tuples
searched_str = self.item['matrix']#searched string
final_tuple_list = []#future drop down list of touple
list_of_rest_tuples = []#the rest of tuples in a list
for x in tup_list:
if x[0] == searched_str:
final_tuple_list.append(x) #touple that should be first
else:
list_of_rest_tuples.append(x)
#append first value that is selected one
final_tuple_list = final_tuple_list + list_of_rest_touples#add Index[0] and rest
self.tb_matrix.items = final_tuple_list
´´´
That’s a bit roundabout. You don’t want to reorder the list, you want to set the selected value:
tup_list = [(r['matrix_dd'], r) for r in app_tables.dropdown.search() if r['matrix_dd'] is not None]#list of tuples
searched_str = self.item['matrix']#searched string
selected_value = None
for x in tup_list:
if x[0] == searched_str:
selected_value = x
self.tb_matrix.items = tup_list
self.tb_matrix.selected_value = selected_value