1

I have a ng-repeat filtered by the parameters city & first_name, as in the example:

vm.search = {
    "city": "D.C.",
    "first_name": "Chu"
};

<li ng-repeat="item in user | filter: search">...

var appname = 'app';
var dependencies = [];
angular.module(appname, dependencies);

var app = angular.module(appname);

var userC = function ($scope) {
	var vm = $scope;

    vm.search = {
        "city": "D.C.",
        "first_name": "Chu"
    };
  
    vm.user = [
        {
            "id": 1,
            "first_name": "Chuck",
            "last_name": "Norris",          
            "city": "Washington D.C.",
            "languages": [
                {
                    "id": "41",
                    "name": "English"
                },
                {
                    "id": "73",
                    "name": "Japanese"
                }
            ]
        }
    ];
};

app.controller('userC', userC);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<ul ng-app="app" ng-controller="userC">
    <li ng-repeat="item in user | filter: search track by item.id">
        {{ item.first_name }} {{ item.last_name }}
    </li>
</ul>

But now I need to filter by languages which is an array of objects:

vm.search = {
    "languages": [
      {
        "name": "Japanese"
      }
    ]
};

<li ng-repeat="item in user | filter: search">...

var appname = 'app';
var dependencies = [];
angular.module(appname, dependencies);

var app = angular.module(appname);

var userC = function ($scope) {
	var vm = $scope;

    vm.search = {
        "languages": [
          {
            "name": "Japanese"
          }
        ]
    };
  
    vm.user = [
        {
            "id": 1,
            "first_name": "Chuck",
            "last_name": "Norris",          
            "city": "Washington D.C.",
            "languages": [
                {
                    "id": "41",
                    "name": "English"
                },
                {
                    "id": "73",
                    "name": "Japanese"
                }
            ]
        }
    ];
};

app.controller('userC', userC);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<ul ng-app="app" ng-controller="userC">
    <li ng-repeat="item in user | filter: search track by item.id">
        {{ item.first_name }} {{ item.last_name }}
    </li>
</ul>

As you can see, there are no results because the default filter doesn't do the job.

Can someone help me achieving a filter that suits this last case?

Both examples are here:

  1. http://codepen.io/anon/pen/KVvQrq
  2. http://codepen.io/anon/pen/JGypqx
1

1 Answer 1

1

You have to declare languages as an object:

vm.search = {
    "languages":
      {
        "name": "Japanese"
      }
};

Here's the codepen.

Update after comment: if you want a more complex filter, you can use a function instead of an object. Angular will call Array.prototype.filter() on your user array, so your function could be defined this way:

var allowedLanguages = [
  "Japanese",
  "Brazilian Portuguese"
];

vm.search = function(item) {
  for (var i = 0; i < item.languages.length; i++) {
    if (allowedLanguages.indexOf(item.languages[i].name) !== -1) {
      return true;
    }
  }

  return false;
};
Sign up to request clarification or add additional context in comments.

3 Comments

T H A N K. Y O U. so simple and clean. I guess the default angularjs filter works only with objects?
Edit: Sorry but with this method I'm not able to filter by multiple languages. For example: I need to filter users having either "Japanese" or "Brazillian Portuguese" as languages.
I'll have a look as soon as I can, thank you for your time!

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.