2016-04-10 50 views
1

我正在使用AngularJS UI-Grid,并试图过滤定价。对于小数点后面的0以外的任何数字,过滤都可以正常工作。

$scope.GridOptions = { 
      enableFiltering: true, 
      rowEditWaitInterval: -1, 
      multiSelect: true, 
      enableColumnMenus: false, 
      columnDefs: [ 
       { name: 'Name', field: 'Item', width: '15%' }, 
       { name: 'Price', field: 'Price', type: 'number', cellFilter: 'currency', width: '6%' }, 
       { name: 'Current Price', field: 'CurrentPrice', type: 'number', cellFilter: 'number: 2', width: '12%' } 
      ] 

2 after decimal0 after decimal

+1

角js有一个内置的货币过滤器。在模板中,您可以执行类似{{item_price |货币}},在ng-repeat内部,如果你需要的话(它看起来像你) – Peege151

+0

它读取它作为货币,但它仍然过滤掉任何会读取'.0'的东西。 – Thecor

回答

1

更改字段类型为每文档“numberstr” @​​的细胞“类型”属性

+0

它仍然失去价格中具有“.0”(即0.00或10.05)的任何东西。在键入小数点的那一刻,它会过滤掉小数点后面有0的任何数据。 – Thecor

+0

-_-我找到了问题的原因。我正在从截断尾部0的CSV(所以任何进来的0.00或10.00将只是0或10)。我使用AngularJS在最后附加.00,但是当您过滤时,UI-Grid将忽略对对象所做的任何图形修改。谢谢您的帮助。 – Thecor

+0

因此js将其解释为一个字符串。您始终可以使用parsefloat()函数附加小数 –

0

我有过太多的跳火圈得到一个漂亮的货币专栏,但希望这将在未来进行核算。这对我来说最好的工作:

JS - 添加cellFilter:number:2cellClass:'currency'到列定义:

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

CSS - 这两种样式定义添加到您的CSS:

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

最终结果:

​​

你可以在我的原始答案here中阅读更多内容。