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
ghuroo
  • 318
  • 2
  • 10
  • Use filter function instead. see http://stackoverflow.com/questions/16563018/angularjs-custom-filters-and-ng-repeat – sdfacre Jan 13 '16 at 23:16

1 Answers1

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;
};
Samir Aguiar
  • 2,509
  • 2
  • 18
  • 32
  • **T H A N K. Y O U.** so simple and clean. I guess the default angularjs filter works only with objects? – ghuroo Jan 14 '16 at 00:15
  • 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. – ghuroo Jan 14 '16 at 00:44
  • I'll have a look as soon as I can, thank you for your time! – ghuroo Jan 14 '16 at 12:14