16

I have huge sets of numeric data. this needs to be rendered as comma separated value. For Ex. 123456.78 to be rendered as 123,457 using Ag-Grid. Kindly help me on achieving this.

thor
  • 21,418
  • 31
  • 87
  • 173
user1638445
  • 161
  • 1
  • 1
  • 5
  • check this, i guess it will help http://stackoverflow.com/questions/1990512/add-comma-to-numbers-every-three-digits – zyz82 Apr 07 '16 at 09:54
  • Thanks for your response, we have **cellFilter:'number:"":0'** in ui-grid of angular js. So, it would be helpful, finding some builtin function available in Ag-Grid. – user1638445 Apr 07 '16 at 11:08

3 Answers3

20

As per the cell rendering flow documentation (here), you can use the colDef.valueFormatter, like this:

    var columnDefs = [
        {headerName: "Number", field: "number"},
        {headerName: "Formatted", field: "number", valueFormatter: currencyFormatter}
    ];
    
    function currencyFormatter(params) {
        return '£' + formatNumber(params.value);
    }
    
    function formatNumber(number) {
        // this puts commas into the number eg 1000 goes to 1,000,
        // i pulled this from stack overflow, i have no idea how it works
        return Math.floor(number).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
    }

You could also use a cellRenderer as other posts describe, but that's typically for more complex rendering, whereas the valueFormatter is specifically for this case. From the ag-grid documentation:

valueFormatter's are for text formatting. cellRenderer's are for when you want to include HTML markup and potentially functionality to the cell. So for example, if you want to put punctuation into a value, use a valueFormatter, if you want to put buttons or HTML links use a cellRenderer. It is possible to use a combination of both, in which case the result of the valueFormatter will be passed to the cellRenderer.

Bernardo Marques
  • 925
  • 2
  • 10
  • 33
Andrew
  • 2,368
  • 1
  • 23
  • 30
  • `currencyFormatter(params: any) { return Math.floor(params.value) .toString() .replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); }` If you want to do it in React Ts – Harsh Phoujdar Mar 15 '19 at 11:46
16
{
         headerName: 'Salary', field: 'sal'
        cellRenderer: this.CurrencyCellRenderer
       }

private CurrencyCellRenderer(params:any) {

    var usdFormate = new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: 'USD',
        minimumFractionDigits: 4
    });
    return usdFormate.format(params.value);
}

Like these we can mention in Angular2 Typescript code.

Nunna Suma
  • 469
  • 1
  • 11
  • 23
3

You can do this by writing a "customcellRenderer", when you create a column definition provide a function to "cellRenderer " attribute and in renderer use number filter, something like this

var colDef = {
    name: 'Col Name',
    field' 'Col Field',
    cellRenderer: function(params) {
        var eCell = document.createElement('span');
        var number;
        if (!param.value || !isFinite(param.value)) {
            number = '';
        } else {
            number = $filter('number')(param.value, 0);
        }
        eCell.innerHTML = number;
        return eCell;
    }
}
Sharpie
  • 31
  • 3
  • 5
    there are value formatting functions such as valueFormatted. aren't those there to accomplish this? seems crazy to have to provide entire rendering function every time you want to format a string – Sonic Soul Nov 21 '16 at 22:27