AG-grid Decimal Precision based on the values of the data

柔情痞子 提交于 2019-12-24 04:38:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!