1

I have a simple ng-repeat:

<li ng-repeat="data in myData.Entries">

  {{ data.EntryNumber }} --

  <strong>From</strong> {{ data.InfoDetails[0].FromDt.substring(6, data.InfoDetails[0].FromDt.length - 2) | date:'MMM yyyy' }} <strong>to</strong>
  {{ data.InfoDetails[0].ToDt == null && 'current' || data.InfoDetails[0].ToDt.substring(6, data.InfoDetails[0].ToDt.length - 2) | date:'MMM yyyy'}}

</li>

Which displays (as expected):

E1 -- From Nov 2011 to current
A1 -- From Jun 2010 to Nov 2011
X1 -- From Apr 2009 to Jun 2010
Z1 -- From Sep 2003 to Jun 2004 

Can i filter this data to only include items that are less than 3 years old (from the current year).

My JS:

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';

  $scope.myData = {

    "Entries": [{
        "EntryNumber": "E1",
        "InfoDetails": [{
          "FromDt": "/Date(1320912000000)/",
          "ToDt": null,
          "RegisteredAs": {
            "SurName": "test",
            "Title": "MRS"
          },
          "RegisteredWith": "test"
        }]
      }, {
        "EntryNumber": "A1",
        "InfoDetails": [{
          "FromDt": "/Date(1276153200000)/",
          "ToDt": "/Date(1320912000000)/",
          "RegisteredAs": {
            "SurName": "test",
            "Title": "MR"
          },
          "RegisteredWith": "test"
        }]
      }, {
        "EntryNumber": "X1",
        "InfoDetails": [{
          "FromDt": "/Date(1239346800000)/",
          "ToDt": "/Date(1276153200000)/",
          "RegisteredAs": {
            "SurName": "test",
            "Title": "MR"
          },
          "RegisteredWith": "test"
        }]
      }, {
        "EntryNumber": "Z1",
        "InfoDetails": [{
          "FromDt": "/Date(1063177200000)/",
          "ToDt": "/Date(1086850800000)/",
          "RegisteredAs": {
            "SurName": "test",
            "Title": "MR"
          },
          "RegisteredWith": "test"
        }]
      }

    ]
  }

});

Heres a plunker: http://plnkr.co/edit/xscddt6AlhxUYIVIklQy?p=preview

1 Answer 1

3

Forked your plnkr

app.filter('filterList', function() {
    return function(items) {
        var retn = [];

        angular.forEach(items, function(item){
            if((new Date().getFullYear()) - (new Date(+item.InfoDetails[0].FromDt.substring(6, item.InfoDetails[0].FromDt.length - 2)).getFullYear()) >= 3){
              retn.push(item); 
            }
        });

        console.log(retn);
        // return items.slice().reverse();
        return retn;
    };
});

Hope this will help

Sign up to request clarification or add additional context in comments.

4 Comments

@dhavlcengg - thanks, but the output list hasnt updated?
I have compared that fromdt should be 3 year old. if you change it to some larger value you will find diff in output list.
can this be based on todt instead?
Yes, it can be. You can do anything based on your requirement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.