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.

As filters provided by AngularJS only work with arrays but not with objects, i'm using the filter function suggested in this solution.

Now i want to extend it, because my json data additionally has a settings-object storing the visibility data for the filtering (unfortunately i can not modify the json structure):

$scope.data = {
      "groups":{
        "1": {
          "type": "foo",
          "name": "blah", 
          "settings": {
            "visibility":[true]
          }
        },
        "2": {
          "type": "bar", 
          "settings": {
            "visibility":[false]
          }
        }
      }
}

Therefore also my filter call is more complex, but of course does not work with the filter at the moment:

<div ng-repeat="(key, value) in data.groups | objectByKeyValFilter:'settings.visible[0]':true>
    {{key}} {{value.type}}
</div>

Probably

objectByKeyValFilter:'settings.visibility[0]' : true

becomes wrongly something like that

myObject['settings.visibility[0]']

How can i modify the filter function in order to achieve the filtering?

Not working Plunker: http://plnkr.co/edit/f202lA?p=preview

share|improve this question

2 Answers 2

What about a bit different approach like this : plnkr

 <div ng-repeat="(key, value) in data.groups ">
       <span ng-show="value.settings.visible">
        {{key}} {{value.type}}
        <span>


      </div> 
share|improve this answer
    
Thanks, for the setting of visibility your approach works – But as soon as i store really complex settings (like template data or meta data) this solution does not work anymore... –  katzenfresser Jun 24 '14 at 12:00

Ok let's have a looke here http://plnkr.co/edit/MgltNXw0x2KWcmWm6QeA?p=preview

<div ng-controller ="test">
      <div ng-repeat="(key, value) in data.groups | objectByKeyValFilter:'settings.visible':'true'">
        {{key}} {{value.type}}
      </div> 
    </div> 

true or false has to be inside quote marks

JS:

angular.module('app').filter('objectByKeyValFilter', function() {
  return function(input, filterKey, filterVal) {


    var filteredInput = [];
    angular.forEach(input, function(value, key) {

      if (value.settings.visible == filterVal) {


        filteredInput.push(value);
      }

    });

    return filteredInput;
  }
});
share|improve this answer
    
yeah...! would be cool, when if(value.settings.visible == filterVal) works for every key instead of only for visible or not... –  katzenfresser Jun 24 '14 at 14:51
    
So that solve you problem ? Or not? 😄 –  sylwester Jun 24 '14 at 15:04

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.