• [转]Format a ui-grid grid column as currency


    本文转自:https://stackoverflow.com/questions/27747184/format-a-ui-grid-grid-column-as-currency-rc-3-0

    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' }}
        ]
    };
    





    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

    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.

  • 相关阅读:
    sort函数详解
    C++重载运算符的规则详解
    poj3061-subsequence
    员工管理系统 :练习
    Jdbc 模拟登陆系统的练习
    有关String 的常用方法
    浅谈希尔排序-----摘录
    简单选择排序
    typedef 和define 的区别
    写博客的理由
  • 原文地址:https://www.cnblogs.com/freeliver54/p/7389277.html
Copyright © 2020-2023  润新知