Searching a table on different combinations of columns

I am trying to search/filter a table in a datagrid using different combinations of say 4 fields or columns. With 4 fields there is about 15 different combinations I think.

I have programmed the search using ‘if’ statements and it works OK but if I want to add more search fields, the code becomes extensive to cover all the possible combinations. I used drop-downs with blank placeholder to represent that I do not want to filter on that particular field.

What is the best way of tackling this type of search to minimise coding and maximise the search power please?

Difficult to say for definite without seeing some code but one useful technique is to build a dict of search arguments and then pass that (unpacked) dict to the search function.

Thanks for your response - I have tried the example in the Anvil Docs:

kwargs = {'age': 42, 'employed': True}
filtered_table = app_tables.people.search(**kwargs)

This works as the fields are numeric and True/False which uses equals (=)

I need to search on text fields with q.like for partial and whole words e.g. ‘%’ + Name +‘%’. Any suggestions would be welcome.

1 Like

Then put those terms into your dict. Nothing to stop you putting q functions as values in there.

1 Like

Here is an example that could help: Search within search - #2 by stefano.menci

Hi Thank you - this works great!

Thanks for your help