Format a ui-grid grid column as currency (RC 3.0)
Asked Answered
S

6

15

The grid options below display the data as expected. But If I try to to format the row.entity[col.field] value in my cellTemplate, I don't get any data returned.

Code:

$scope.gridOptions = {
    enableSorting: true,
    columnDefs: [
        {name: 'Award Title', field: 'projectTitle', minWidth: 100 },
        {name: 'Amount', field: 'awardAmount', cellTemplate: '<div>{{Number(row.entity[col.field]).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')}}
    ]
};

Any guidance on how to format the column as currency is appreciated.

Thanks.

Sisterhood answered 2/1, 2015 at 19:2 Comment(2)
Are you getting any errors in the console? Not sure what the purpose of the replace() part of your expression is as toFixed() should give you what you want, but that doesn't seem to be the issue here.Fugato
No errors in the console. My objective is that if I get back '1234' for the amount, I want it formatted as $1,234.00.Sisterhood
B
20

You can use 'currency' cellFilter to format your data.

$scope.gridOptions = {
    enableSorting: true,
    columnDefs: [
        {name: 'Award Title', field: 'projectTitle', minWidth: 100 },
        {name: 'Amount', field: 'awardAmount', cellFilter: 'currency' }}
    ]
};
Bushman answered 3/1, 2015 at 4:13 Comment(1)
This is the simplest way rather than coding your own. You can also customize for currency symbol and number of decimal places like this: cellFilter: 'currency:"£" : 5' Lyndel
S
11

Have a look at this nice article : http://brianhann.com/6-ways-to-take-control-of-how-your-ui-grid-data-is-displayed/

In summary your options are :

  • Bindings
  • Cell Filters
  • Cell Templates
  • Links
  • Buttons
  • Custom directives

I have used the cell Filter option myself (code translated to your case, not tested):

$scope.gridOptions = {
   enableSorting: true,
   columnDefs: [
      {
          name: 'Award Title', 
          field: 'projectTitle', minWidth: 100
      }, {
          name: 'Amount', 
          field: 'awardAmount', 
          cellFilter: 'currencyFilter'
      }
   ] 
};

With filter defined hereunder :

app.filter('currencyFilter', function () { 
    return function (value) {
        return value.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
    }
});    

Cheers, G

Sexuality answered 27/3, 2015 at 8:18 Comment(0)
A
4

I'm late to the party, but wanted to share the best thing that worked for me:

JS:

myGrid.columnDefs = [{
    field: "Sale",
    cellFilter: 'number:2',
    cellClass:'currency'
}]

CSS:

.currency{
    text-align:right;
}
.currency .ui-grid-cell-contents:before{
    content: '$';
    float: left;
}

Final result:

The final currency column


Details:

First I tried using cellFilter:'currency' as mentioned in a previous answer, but it didn't allow for various dollar amounts and didn't align the values to the right:

Using only the currency filter.

So then I added a currency class, and right-align, which fixed alignment, but not different dollar amounts.

Added alignment.

Unfortunately, I wasn't able to find an easy fix for this (although I feel like there is one out there somewhere), so I had to change the cellFilter from currency to number:2 - this means "Always show 2 decimals".

Show 2 decimals

Then I wanted a dollar sign at the left of the cell (to avoid varying dollar amounts), for that I added the following css:

.currency .ui-grid-cell-contents:before{
    content: '$';
    float: left;
}

Which left me with the final result:

Final column, right-aligned and compensating for various dollar amounts.

Addieaddiego answered 9/8, 2017 at 23:7 Comment(0)
O
2

You can use cellFilter attribute and default angularJS currency filter Live Demo - http://plnkr.co/edit/NSiCrM?p=preview

$scope.colDefs = [
    { field: 'firstName', 
      displayName: 'FN' 
    },
    { field: 'amount',
      displayName: 'AMT', 
      cellFilter: 'currency'
    }
  ];
Originality answered 13/7, 2017 at 11:54 Comment(0)
S
1

Try something like this, this apply angular native filters

cellTemplate: "<div>[[ row.entity[col.field]  | currency:'Q':2 ]]"
Scaleboard answered 5/3, 2015 at 13:13 Comment(0)
F
0

Well take your $ out of the regular expression for starters. Just put it after the tag. The expression, apart from the $ part, works. It looks like you are just trying to call a Javascript function within the {{ }} rather than calling something in your controller. Move the Number().toFixed().replace into a function in your controller and call that instead

     $scope.asCurrency = function(val) {
       return Number(...).toFixed()......
     };
Fugato answered 2/1, 2015 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.