I am needing help with stuff to do with the dropdown boxes. So I have a database with many columns in it. When I try to populate my dropdown with the columns I cannot do it because there are too many columns. There is 145 total. To try and work around this, I want to enter a certain word in a textbox, and for the dropdown to search the data table and populate the dropdown with columns in the data table which matches the word I have entered. I am quite new to anvil and my knowledge is low so can please help me out and direct me where to start.
Hi @mitchjam ! Welcome to the Anvil Community!
First of all, I moved your topic to Anvil Q&A. Since this is a question, it’s more fitting here. When you developed a cool app and you want to show it and tell more about it to the community, then you use the Show & Tell category.
Okay, about your question now: The first question I need to ask you is: are you sure you have 145 columns? Are you not refering to rows?
If you are talking about rows, it’s pretty easy to do. Follow this steps:
-
Create a Server Module if you don’t have it yet. Server Modules are used to put code that is run in the server side, where your data tables are, for instance (Mode about Server Code: Anvil Docs | Server Code);
-
Inside your server module, create a new function. This function must receive a parameter which will be what your text_box will provide and it will return a list of options from your data table accordingly. So the name of the function should be something that can tell you right away what is doing, like “search_table_name”, where table name is the name of your data table;
-
Add the annotation for server callable above your function to make it possible to call from your client code. You should add
@anvil.server.callable
. More about this in Anvil Docs | Server Code. -
This function will return then a search iterable for the table. Your function should look like this:
@anvil.server.callable
def search_table_a(text_param:str):
return app_tables.table_a.search(ColumnToSearch=text_param)
Explaining: You will be receiving the text_param
from client code, from your text box. Then, you will return a search in the table_a, looking in the column name ColumnToSearch by some row that has the exact same text. This function will return a iterator (if you don’t know what this is, think of a list of objects) of all rows that match the search. If you want your search to be more inclusive and search with uncompleted values (find “Unhappy” by typing “happy” or things like that) you can replace text_param
by q.ilike("%" + text_param + "%)
. More on this here: Anvil Docs | Storing Data in Data Tables.
- Then, from your client code you can use this function to search the values. Let’s say you have a text_box named text_box_1, a button to click and search the values named button_1 and a dropdown where to show the values named dropdown_1. You then can bind your Button click to the following code:
def button_1_click(**event_args**):
self.dropdown_1.items = anvil.server.call('search_table_a', self.text_box_1.text)
Explaining: When you click the button, you will set the itens of your dropdown to be the result of the function. Anvil.server.call will search for a function that has the @anvil.server.callable
above it in server modules and run it, passing the parameters that are listed after the string, so the self.text_box_1.text
. This parameter will be the text in the text box, that will be used in the search.
You can read more about dropdowns and data tables here: Anvil Docs | Dropdowns and Data Tables.