问题
I have some decimal value columns in a grid where I want to have a variable precision based on the values of the decimal. Specifically, decimal places of precision should be 0 if the value is > 9.5, precision should be 1 if the number is between 0.5 and 9.5 & 2 otherwise. Currently, I am defining the precision in the column definitions like this:
"columns": [
{
"name": "exposure",
"type": "DECIMAL",
"title": "Exposure",
"precision": 2,
"width": 77
},
{
"name": "total_risk",
"type": "DECIMAL",
"title": "Total Risk",
"agg_name": "Total Risk",
"precision": 0,
"width": 78
}]
Does ag grid provide any functionality like this. I don't see anything in the official documentation
回答1:
You can handle displaying values via valueFormatter
in columnDef
columnDefs = [
{
headerName: "Sample data",
field: "sampleNumber",
valueFormatter: numberFormatter,
width: 200
}
];
...
numberFormatter(params){
// params.data - full row data
// params.value - cell value
// here you can handle how certain cell data would be displayed on the grid
return Number(params.value.toFixed(params.data.samplePrecision))
}
Here is a working plnkr sample
Official docs for Value Setters
& Value Parsers
回答2:
Here is another example that should work using Angular 8:
columnDefs = [
{
field: 'charge',
headerName: 'Charge',
valueFormatter: this.currencyFormatter,
precision: 3
}
]
currencyFormatter(params: any) {
return params.value.toFixed(params.colDef.precision);
}
来源:https://stackoverflow.com/questions/52316196/ag-grid-decimal-precision-based-on-the-values-of-the-data