Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ag-grid - filter string as numeric value without changing the data type

I have an ag-grid table with a column defined as follows:

{ headerName: 'Population', field: 'properties.population', sortable: true, filter: 'agNumberColumnFilter'}

The data i receive is for population is a string with a numeric value inside, for example:

[
{
"properties": {
        "state": "Alabama",
        "name": "Montgomery",
        "population": "199518"
      }
},
{
"properties": {
        "state": "Alaska",
        "name": "Juneau",
        "population": "32094"
      }
}
]

Which means the filter won't work.

I worked around this by changing the data in the component

this.data.forEach(item => {item.properties.population= Number(item.properties.population)});

Is there another solution in the field of ag-grid to change the filter behavior in filterParams or something else to avoid this data manipulation?

like image 959
Ventura8 Avatar asked Feb 16 '26 17:02

Ventura8


1 Answers

I managed to solve this via valueGetter in the column def

{ headerName: 'Population', 
field: 'properties.population', 
sortable: true, 
filter: 'agNumberColumnFilter',
valueGetter: (params) => { return parseInt(params.data.population) }
}

More information about valueGetter in ag-grid documentation: https://www.ag-grid.com/javascript-grid-value-getters/#example-getters-and-formatters

like image 56
Ventura8 Avatar answered Feb 18 '26 07:02

Ventura8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!