Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array (_users) that contains JSON objects.

{ 
  "User":
  {
    "userid":"19571",
    "status":"7",
    "active":"1",
    "lastlogin":"1339759025307",
    "Stats":
     [
       {
         "active":"1",
         "catid":"10918",
         "typeid":"71",
         "Credits":
         [
           {
             "content":"917,65",
             "active":"1",
             "type":"C7"
           },               
           {
             "content":"125,65",
             "active":"1",
             "type":"B2"
           }
         ]
       }
     ]
  }
}

1- How can I filter only users with "active":"1" not "0"

I have tried something like this:

<div ng-repeat="user in _users | filter:active | orderBy:user.userid">
    {{user.userid}}
</div>

but not working correctly for me.

Thank you!

share|improve this question

2 Answers 2

up vote 25 down vote accepted

Since your object model is kind of complex I would recommend using custom filtering function:

$scope.isActive = function(user) {
    return user.User.Stats[0].active === "1";
};

and then in your HTML:

<div ng-repeat="user in _users | filter:isActive">
    {{user.User.userid}}
</div>

Here is the working jsFiddle: http://jsfiddle.net/pkozlowski_opensource/4kzzy/3/

Be sure to check angular's documentation on filters if you need more info: http://docs.angularjs.org/api/ng.filter:filter

share|improve this answer
    
This works perfect. Thank You! –  UR13X Aug 12 '12 at 15:24
1  
Answer above is perfect but FYI, if you want to filter your array before binding to scope, you can also use jQuery.grep() or the _filter from underscore.js jsfiddle.net/NRuM7/1 - jQuery.Grep() documentcloud.github.com/underscore/#filter - underscore.js –  GFoley83 Mar 16 '13 at 4:23

Taking @pkozlowski.opensource a bit further and allowing live filtering of items/users:

http://jsfiddle.net/hbyNN/

<div ng-controller="MyCtrl">
  <label>userid substring filter:</label>
  <input ng-model="zzzzz">

  <div ng-repeat="user in _users | filter:isInteresting"> 
      <pre>{{user | json}}</pre>
  </div>
</div>

And:

   $scope.isInteresting = function (user) {
    if ($scope.zzzzz == undefined) {
        return true;
    }

    return user.userid.indexOf($scope.zzzzz) !== -1;
};
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.