How do you fill the items of a DropDown from a data table?

Hi @nratner,

You’re actually really close! If you’re searching a data table for a particular column value, you need to search with a value of the right type. In this case, as you’re searching the category column of app_tables.anomaly_category_and_type, which is a link to a row from app_tables.anomaly_category, you should pass in the actual row object to search().

I’ve modified your example so it works:

https://anvil.works/ide#clone:OFZKMHNUEHYXS3KS=WKLUFIWGQNA7AMHLYSUN43VO

I made the following changes:

  • When the server functions return item lists for DropDowns, I return tuples of (<string label>, <actual value>). The DropDown will display the label string, but selected_value will be the actual value (in this case, the row object itself).

  • This means that when we’re calling anomaly_type_group(), we’re passing in an actual row from the anomaly_categories table - not just the string from its first column. This means we can search correctly in the many-to-many table.

  • I also cleaned up a couple of tiny bugs (eg you’d clearly renamed self.anomaly_type_choice since you wrote line 23 of Form1, so I changed it :). You also didn’t have the change event wired up for the anomaly_category_choice dropdown, so I did that for you. I’m pretty sure you would have fixed these two bugs very quickly once you had the tables stuff sorted.)


I also have one recommendation – it will probably be helpful if you rename the first column of the “Anomaly Category” and “Anomaly Type” tables. I like to call that column 'name'`. This makes it clear in your code whether you’re referring to a category (ie a row from that table) or the name of a category (a string). This helps with problems like the one you had here.

Here’s a version of that app with those columns renamed, to show what I mean:
https://anvil.works/ide#clone:2Y3MX4JXGRTF3CMP=EJOVTKBQCXWP6T25LU3ICI5X

I hope that helps! Please do keep asking questions (and thank you to everyone else who’s been chipping in!)

1 Like

Hi David,

You pointed out exactly what I was trying to resolve. I am passing a string but the table is looking for a “sys value” of the result rather than the string. I have run into this with other platforms where a value has a “sys_id” in the background.

…and yes, I do know SQL libraries much better. Thank you again for your help and no rush as I am just evaluating Anvil as a possible replacement for our current solutions.

Thank you Meredydd! I will look through your code and get a better understanding of the implementation. On first, pass, the app works exactly as expected with the table structure/pattern I put in place.

Don’t worry, I’m sure I will have more questions :slight_smile:

There’s a reason he went to Cambridge :slight_smile:

Update: I have now expanded my answer in this thread into a new walk-through video for our “Cookbook” section:

The source code link on the cookbook page is broken…

Oops! That’ll be fixed in the next 24 hours.

A post was split to a new topic: DropDown Items syntax error