'Force ag-grid floating filter to only accept numbers in input

I'm using ag-grid enterprise and I would like to apply floating filters for all my columns. Sometimes the filter can be alphanumeric but in other cases it should only accept numbers.

I tried to manage this following this example from the ag-grid doc:

const gridOptions = {
    columnDefs: [
        {
            field: 'age',
            filter: 'agNumberColumnFilter',
            filterParams: {
                allowedCharPattern: '\\d\\-\\,', 
                numberParser: text => {
                    return text == null ? null : parseFloat(text.replace(',', '.'));
                }
            }
        }
    ],

    // other grid options ...
}

but it doesn't work... I was wondering if an extra configuration is needed for the allowedCharPattern to work.

This is how a colDef looks like in my project:

{
                    ...commonProperties,
                    field: column.fieldName,
                    suppressSizeToFit: false,
                    sortable: true,
                    resizable: true,
                    minWidth: getMinWidth(column.fieldName),
                    width: column.colWidth,
                    filter: 'agTextColumnFilter',
                    floatingFilter: true,
                    floatingFilterComponentParams: {
                        suppressFilterButton: true,
                    },
                    filterParams: {
                        allowedCharPattern: '\\d\\-\\,',
                        numberParser: (text) => {
                            return text == null
                                ? null
                                : parseFloat(text.replace(',', '.'))
                        },
                    },
                }

Am I missing something? thanks in advance



Solution 1:[1]

  • Use "agNumberColumnFilter" instead of "agNumberColumnFilter"
  • Please check if you are using latest version of ag grid. It was not working for me on older version. It started working after upgrading to latest version 27.2.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shilpa