简体   繁体   English

如何使用AngularJs过滤JSON数据?

[英]How to filter JSON-Data with AngularJs?

I have an array (_users) that contains JSON objects. 我有一个包含JSON对象的数组(_users)。

{ 
  "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" 1-如何仅过滤“有效”的用户:“1”不是“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! 谢谢!

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: 然后在你的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/ 这是工作的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 如果您需要更多信息,请务必检查过滤器上的角度文档: http//docs.angularjs.org/api/ng.filter :filter

Taking @pkozlowski.opensource a bit further and allowing live filtering of items/users: 将@ pkozlowski.opensource更进一步,并允许对项目/用户进行实时过滤:

http://jsfiddle.net/hbyNN/ 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;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM