What I’m trying to do:
I’m trying to filter a data grid with a MultiSelectDropDown from anvil_extras. This component returns a list of strings, but when I pass the list with two or more strings to my function, the data grid doesn’t show any row.
What I’ve tried and what’s not working:
I’ve tried to iterate over the list of strings called brand (brand = self.multi_select_drop_down_1.selected), and append the result to a list of queries.
Code Sample:
queries = []
if brand:
for a in brand:
queries.append(q.any_of(brand=q.ilike('%{}%'.format(a))))
if len(queries) > 0:
result = app_tables.email.search(q.all_of(*queries))
else:
result = app_tables.email.search()
Thank you for your feedback. The problem with result = app_tables.email.search(q.any_of(*queries)), is that works with filtering by multiple items but when I add other filters it doesn’t.
My mistake, I didn’t share the whole function:
@anvil.server.callable
def search_table(query=None, brand=None, country=None, language=None, from_date=None, to_date=None):
queries = []
if query:
queries.append(
q.any_of(
subject=q.ilike('%{}%'.format(query)),
brand=q.ilike('%{}%'.format(query)),
country=q.ilike('%{}%'.format(query)),
language=q.ilike('%{}%'.format(query))
)
)
if brand:
for a in brand:
queries.append(q.any_of(brand=q.ilike('%{}%'.format(a))))
if country:
queries.append(q.any_of(country=q.ilike('%{}%'.format(country))))
if language:
queries.append(q.any_of(language=q.ilike('%{}%'.format(language))))
if from_date:
queries.append(q.any_of(email_date=q.greater_than_or_equal_to(from_date)))
if to_date:
queries.append(q.any_of(email_date=q.less_than_or_equal_to(to_date)))
if len(queries) > 0:
result = app_tables.email.search(q.all_of(*queries))
else:
result = app_tables.email.search()
return result
I’ve used the same technique for flexible queries without any issues, and I don’t see anything in your code that particularly jumps out at me as wrong.
What’s the structure of the data table you’re searching? Are any of the fields linked fields instead of strings?