Ag grid bool column cannot filter

I have bool columns in my dataframe I am sending to ag grid. They show as ‘true’ or ‘false’ . That is fine but I cannot filter on either value. The filter will not allow me to type anything. For my numeric columns I set type to ‘numericColumn’. Is there something like ‘boolColumn’ or ‘booleanColumn’ for bool that will make the filtering work? I guess I could convert the dataframe bool columns to strings and then I could probably filter. Converting makes things messy when saving table changes to the database because I have to convert back…

Hello @Brent,

Have you tried 1 for True and 0 for False?

Okay, I figured out what is going on. I have a generic wrapper to build ag_grid tables from a dataframe. I test the pandas dataframe columns to see if I need to make the ag_grid column type definition ‘numericColumn’. To test the columns I use something like this:

import pandas.api.types import is_numeric_dtype, is_float_dtype, is_bool_dtype

def ag_grid_df_column_type(df_column):
    """Determine the dataframe column type.
       Note: This only works with pandas >= 1.0.0
    return 'numericColumn' if is_numeric_dtype(df_column) else None

    for ii, col in enumerate(dataframe.columns):
        grid_col = {'field': col,
                    'editable': col in editable_columns,
                    'type': ag_grid_df_column_type(dataframe[col])}

The problem with that is that pandas bool columns are numeric even though they have ‘true’ and ‘false’ in them. So, my test above tells ag_grid the column type is ‘numericColumn’. Filtering does not work because it will only allow me to type numbers. Typing in 0 for false and 1 for true does not match the true or false that shows in the fields. Because the ag_grid column type is numericColumn I cannot type in true or false.

I can make filtering work by making pandas bool columns not be numeric to ag_grid with the following:

def ag_grid_df_column_type(df_column):
    """Determine the dataframe column type.
       Note: This only works with pandas >= 1.0.0
    return 'numericColumn' if not is_bool_dtype(df_column) and is_numeric_dtype(df_column) else None

I am still wondering if there is anything in ag grid like ‘boolColumn’?

Hi @Brent

Try upgrading to Dash AG Grid V31. There is a boolean data type where it renders boolean values as a check box and has a true/false filter. And it’s all automatic as it can infer the data type based on the data.

See this post for more information

1 Like

I finally got a chance to try this. It is a lot nicer. The filter dialog is a little squirrely though.
Once the filter is selected it is hard to get rid of the dialog box. Normally on the other filters pressing the escape key closes the dialog box. It does not seem to work on the boolean filter. The only way the box goes away is by clicking on a different column filter. Am I missing the proper way to get rid of the filter dialog box?

Clicking somewhere else on the page also closes the bool filter dialog box. I have to be careful though because clicking inside the grid selects a row. It would be nice if escape closed the dialog like the other filters.

Hi Brent

Using escape to close the dialog on the boolean filter seems to work. You can see it in this page in the dash docs

You can add buttons with a close on apply options. You can see a live example here (along with some of my other favorite defaults):

An you can find more info in the dash docs: