Datatable - filter for multiple arguments in one column

Hi,

currently I am using the basic column filtering in my datatable but I would like to enable filtering of multiple items in one column. For example if I have the column “fruits” and I would like to filter for all “apple” and “banana” records. A query would look like “‘apple’ OR ‘banana’”.

Is that possible?

Thanks!

1 Like

Hey,

Have you tired using a callback with dcc.Dropdown and multi option?

dcc.Dropdown(id='your_id', options=dropwdown, multi=True)

You can get the list of items from your DataFrame as:

list_of_options = df['column_name'].sort_values().unique().to_list()
dropdown = [{'label': x,  'value': x} for x in list_of_options ]

Then you implement that as a callback in your app.

Let me know if it works.

Daniel

Hi Daniel,

thanks for your suggestion! No I did not yet try your approach since I was hoping that the inbuilt column filter option could be used for my purpose.

I tried your solution and it works (except the filtering is not applied) and I added it to app.layout to have the new dropdown-filter element on top of my datatable-div. How should I implement it in a callback so the filtering does have an effect on the datatable?

Thanks!

Hey,

The callback information can be found at:
https://dash.plotly.com/basic-callbacks

In your case it would be:

@app.callback(Output('table_id', 'data'), [Input('dropdown_id', 'value')])
def update_table(drop_value):
    if drop_value:    
        df = df[df['column_name'].isin(drop_value)]
        return df.to_dict('records')
    return df.to_dict('records')

It should be pretty straightforward.

Daniel