0

I have some problem with filtering nested object. In select I choose the filter parameter(object value) and in input I type some text that searches it in object key.
Tried to write custom filter with recursion for deep search but it doesn't work. input is parameter for object in ng-repeat, param1 is for select's ng-model and param2 is for input's ng-model.


JS

  .filter('personFilter', function($filter) {
    return function(input, param1, param2) {
      var output = {};
      for (var prop in input) {
          if (typeof input[prop] == 'object' || prop != param1 && input[prop] != param2) {
              $filter('personFilter')(input[prop]);
          } else {
              output[key] = input[key];
          }
      }
      return output;
    }


Here's the plunker: http://plnkr.co/edit/83lPNRWFy6wa9U2FkMfH?p=preview
I hope someone give me some advice

1
  • 1
    seems like data format is not valid. you should get an error like Uncaught SyntaxError: Unexpected token : because format can be somehting like [{'node1': {node2: ''}}] not ['node1': {...}] Commented Sep 28, 2016 at 8:37

1 Answer 1

0

sorry, just dirty solution, have no time to refactor. Mb it save time to you

.filter('personFilter', function($filter) {
    return function(input, recursive, search) {
          return input.filter(filterFn);
          function filterFn(obj){
            var val, res;
            for (var prop in obj) {
                val = obj[prop];
                if (typeof val == 'object' && recursive) {
                    recursive = false;
                    res = res || val.filter(filterFn).length;
                    recursive = true;
                } else if(!recursive){
                    res = res || val == search;
                }
            }
            console.log(res, obj, recursive, search);
            return res;
          }

        }

with following markup

<body ng-controller="appCtrl">
    <select ng-model="selectParameter" ng-options="item.value as item.key for item in parameter track by item.value">      
    </select>
    <input ng-model="query" />

    <div ng-repeat="person in object.node1.node2.persons | personFilter:selectParameter:query track by $index">
     <!--   -->
        <p>Person: {{person.name}}, Children: <span ng-repeat="child in person.children track by $index">{{child.name}}, </span></p>
    </div>
  </body>

and controller init like:

$scope.parameter = [
      {value: false, key: 'Person Name'},
      {value: true, key: 'Child Name'}
];
$scope.selectParameter = true;
Sign up to request clarification or add additional context in comments.

9 Comments

what do you mean?
See the plunker
I saw it. What do you mean under the strange?
I doesn't filter correct even in console. If I try to find by First name and type 'Peter' it also shows me 'Martin'. And if I have more than two select options? There are true and false in option values.
|

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.