I am new to angular js and was trying to filter nested json data(basically i want to display date for past few days based on users entry) in angular js using custom filter. Basically I am trying to filter date object in json using custom filter.I am not sure whether we can filter nested object like date object using my current code or i may need to change my current implementation.
I got it working like this before when the date was in string format and it worked fine http://plnkr.co/edit/5IhJYSXvqa5nwd87y8kD?p=preview
But when i tried nested json date format it did not work or other readings in json format i could not make it work. I am trying to figure out a way to filter nested json object(date or other parameters in the data) using the custom filter.Any help would be appreciated. Here is a plunker link that 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>
`