Angular js filter for JSON key/value nested object is not working for me

angularjs json filter

61 观看

2回复

1 作者的声誉

My JSON data looks like the below code. Can you any one please help me to filter data by code/desc which is in the state property.

$scope.agent =
{
    "0d297c1711de":
    [{
        "applicationName": "rewards-accounts", "agentId": "0d297c1711de",
        "status": { "agentId": "0d297c1711de", "eventTimestamp": 1510580172247, "state": { "code": 100, "desc": "Running" } }
    }],
    "16f279d66923":
    [{
        "applicationName": "rewards-accounts-details", "agentId": "16f279d66923",
        "status": { "agentId": "0d297c1711de", "eventTimestamp": 1510580172247, "state": { "code": 201, "desc": "Unexpected Shutdown" } }
    }],
    "203b353d32ef":
    [{
        "applicationName": "rewards-accounts-details", "agentId": "203b353d32ef",
        "status": { "agentId": "0d297c1711de", "eventTimestamp": 1510580172247, "state": { "code": 200, "desc": "Shutdown" } }
    }]
};

I have used this filter in ng-repeat . It is not working. selectedCode is my model data to be filtered.

| filter:{status:{ state: { code: **selectedCode**}}}
作者: Dinesh Babu Mannu 的来源 发布者: 2017 年 12 月 27 日

回应 2


0

1621 作者的声誉

As stated in this SO

You need to pass in the argument to filter by, which in your particular case looks like:

ng-repeat="a in agent | filter: {status: {state: {code: filter.key}}}"

With thanks to @Ray

JSFiddle here for further reference.

Hope it helps.

作者: Jax 发布者: 2017 年 12 月 27 日

0

3461 作者的声誉

You can use a custom filter to filter your nested data,

in your controller create your filter

$scope.customFilter = function (row) {
    var result = false;
    if($scope.filter == undefined || $scope.filter == ""){
          result = true;
    }
    else{
         angular.forEach(row, function(value, key){
           if(value.state != undefined){
              if(value.state.code == $scope.filter)
                result = true;
           }
         });
    }
    return result;
};

and add this filter to your ng-repeat

<div ng-repeat="item in agent">
  <div ng-repeat="x in item | filter:customFilter">
    {{x}}
  </div>
</div>

Demo

作者: NTP 发布者: 2017 年 12 月 27 日
32x32