格式化数据之前使其 [英] Formatting data before render it

查看:129
本文介绍了格式化数据之前使其的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示视图的一些数据,但我需要先格式化,我在做类似

I am displaying some data in the view, but I need to formatted first, I was doing something like

val.toFixed(2)键,这是确定的,它的工作原理,但问题是, VAL 有时来自用字母和 toFixed(2)没有考虑到这一点,因此不显示的字母。

val.toFixed(2) and that is OK, it works but the problem is that val sometimes comes with letters, and toFixed(2) is not taking that into account so is not displaying the letters.

所以我需要的东西,考虑到字母和数字,字母不必改变,只有来自像 234235.345345435 的数字,显然我需要它像这样的 234235.34

So I need something that takes into account letters and numbers, the letters don't have to change, only the numbers which comes like 234235.345345435, and obviously I need it like this 234235.34.

下面是一些我使用code的

Here is some of the code I am using

<table>
    <tr>
        <th ng-repeat='header in headers'>{{header.th}}</th>
    </tr>
    <tr>
        <td ng-repeat='data in headers'>
          <div ng-repeat='inner in data.td'>
            <span ng-repeat='(prop, val) in inner'>{{val.toFixed(2)}}</span>
          </div>
        </td>
    </tr>
</table>

和在控制器

$scope.LoadMyJson = function() {
      for (var s in myJson){
        $scope.data.push(s);
        if ($scope.headers.length < 1) 
            for (var prop in myJson[s]){
            prop.data = [];
            $scope.headers.push({th:prop, td: []});
          }
      }
      for (var s in $scope.data){
        for (var prop in $scope.headers){
            var header = $scope.headers[prop].th;
              var data = myJson[$scope.data[s]][header];                         
                 $scope.headers[prop].td.push(data);
                 console.log($scope.headers[prop].td);
        }
      }
};

和我prepared 这个小提琴

and I prepared this Fiddle

事情是这样的,现在,正确显示表,但如你所见,该表缺少名称,这是因为<$ C $的C> toFixed 方法。

the way it is right now, is displaying the table properly, but as you see, the table is missing the name, it is because of the toFixed method.

所以,我该怎么办?

推荐答案

创建一个自定义过滤在您的模板中使用。

Create a custom filter to use on your template.

<table>
    <tr>
        <th ng-repeat='header in headers'>{{header.th}}</th>
    </tr>
    <tr>
        <td ng-repeat='data in headers'>
          <div ng-repeat='inner in data.td'>
            <span ng-repeat='(prop, val) in inner'>{{val|formatValue}}</span>
          </div>
        </td>
    </tr>
</table>


angular.module('whatever').filter('formatValue', function () {
  return function (value) {
    if (isNaN(parseFloat(value))) {
      return value;
    }

    return parseFloat(value).toFixed(2);
  } 
});

这篇关于格式化数据之前使其的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆