我们可以使用角度js中的自定义过滤器过滤嵌套的json数据

时间:2015-04-12 15:21:31

标签: javascript json angularjs angularjs-ng-repeat angularjs-filter

我是角度js的新手,并试图使用自定义过滤器过滤角度js中的嵌套json数据(基本上我想基于用户输入显示过去几天的日期)。基本上我试图使用自定义filter过滤json中的日期对象。我不确定我们是否可以使用当前代码过滤嵌套对象,如日期对象,或者我可能需要更改当前的实现。

在日期是字符串格式之前我得到了这样的工作,并且工作正常 http://plnkr.co/edit/5IhJYSXvqa5nwd87y8kD?p=preview

但是,当我尝试嵌套的json日期格式时,它无法正常工作或以json格式读取其他读物,我无法使其正常工作。我试图找出一种方法来过滤嵌套的json对象(数据中的日期或其他参数)使用自定义过滤器。任何帮助将不胜感激。 这是一个plunker链接 http://plnkr.co/edit/en36loBKQ2DAnOcbwe8v?p=preview`



var app = angular.module('tempfilter', []);

app.controller('MainCtrl', function($scope) {
  $scope.sensordata = [{
    id: 'id:1',
    name: 'Rob',
    "ValidationDate": {
      "$date": "2015-02-20 18:00:05-0400"
    },
    "Temp": 42
    
    
    
    app.filter('tempo', function() {
  return function(items, field, value) {
    var filtered = [];

    var newdate = new Date().setDate(new Date().getDate() - value);

    angular.forEach(items, function(item) {
      if (new Date(item[field]) > newdate) {
        filtered.push(item);
      }
    });
    return filtered;
  };
});

<body ng-controller="MainCtrl">
  Number of days before today
  <input type="number" ng-model="filter.value">
  <p id="demo">Showing data for last {{ filter.value }} days</p>
  Filtered list:
  <ul>
    <li ng-repeat="s in sensordata | tempo:'ValidationDate.$date':filter.value">{{s.id}} {{s.ValidationDate.$date|date}} {{s.name}} {{s.Temp}}
  </ul>
</body>
&#13;
&#13;
&#13;

`

2 个答案:

答案 0 :(得分:0)

您的约会对象&#39;类型为String,您最好将它们更改为JavaScript中的时间戳或Date对象。

有一些示例代码:

<span ng-non-bindable>{{1288323623006 | date:'medium'}}</span>:
<span>{{1288323623006 | date:'medium'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span>:
<span>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:'MM/dd/yyyy @ h:mma'}}</span>:
<span>{{'1288323623006' | date:'MM/dd/yyyy @ h:mma'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:"MM/dd/yyyy 'at' h:mma"}}</span>:
<span>{{'1288323623006' | date:"MM/dd/yyyy 'at' h:mma"}}</span><br>

答案 1 :(得分:0)

发现我的问题已经存在了很长时间。一个简单的改变是javascript可以做到这一点,嵌套的json数据可以使用。注释如下面的例子中的item.ValidationDate。$ date in plunker。

http://plnkr.co/edit/NEBYrQeaLX84sCSNDCzV?p=preview

&#13;
&#13;
// Code goes here

// Code goes here
//http://stackoverflow.com/questions/18935889/difference-between-date-parse-and-gettime

var app = angular.module('tempfilter', []);

app.controller('MainCtrl', function($scope) {
  $scope.sensordata = [
    {id:'id:1',name:'Rob',"ValidationDate": {"$date":"2015-04-12 18:00:05-0400"},"readings" : [ {"Temp" : 20 }]},
    {id:'id:2',name:'Rob',"ValidationDate": {"$date":"2015-04-13 18:00:05-0400"},"readings" : [ {"Temp" : 25 }]},
    {id:'id:3',name:'Rob',"ValidationDate": {"$date":"2015-04-11 18:00:05-0400"},"readings" : [ {"Temp" : 26 }]}
   
  ];
  
  $scope.filter = { value:100};
    
});

app.filter('tempo', function() {
    return function( items, field, value) {
      var filtered = [];
      
var newdate=new Date().setDate(new Date().getDate()-value);
//var tempp=26-value;

      angular.forEach(items, function(item) {
        //if (new Date(item[field]) > newdate){
        //new logic of accessing value by function
        //if (item.readings[0].Temp >tempp ){
        if (new Date(item.ValidationDate.$date) >newdate ){
          
          filtered.push(item);
        }
      });
      return filtered;
    };
});
&#13;
<!DOCTYPE html>
<html ng-app="tempfilter">
  
  <head lang="en">
    <meta charset="utf-8">
    <title>DateFilter</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
    <script>
      document.write('<base href="' + document.location + '" />');
    </script>
    <script src="script.js"></script>
	
</head>
  
<body ng-controller="MainCtrl">
Number of days before today<input type="number"  ng-model="filter.value">
  <p id="demo">Showing data for last {{ filter.value }} days</p>
  Filtered list:
	<ul>
    <li ng-repeat="s in sensordata | tempo:'Date':filter.value">{{s.id}}
     {{s.ValidationDate.$date|date}}
    {{s.name}}
    {{s.readings[0].Temp}}
  </ul>
	
	Full List:
	<ul>
    <li ng-repeat="s in sensordata ">{{s.id}}
      {{s.ValidationDate.$date|date}}
      {{s.name}}
	    {{s.readings[0].Temp}}
    </li>
  </ul>
</body>

</html>
&#13;
&#13;
&#13;