Nik Kashi
Nik Kashi

Reputation: 4596

Filter angular-ui typeahead autocomplete for more than one field

I have a snippet in plunker. I am try to filter result for two fields(in OR condition)

typeahead="state as state.name for state in statesWithFlags | filter:{name:$viewValue,country:$viewValue}"

instead of current

typeahead="state as state.name for state in statesWithFlags | filter:{name:$viewValue}"

the condition applied as AND term, but I need OR term

Upvotes: 2

Views: 2128

Answers (1)

apairet
apairet

Reputation: 3172

You can create a custom filter:

angular.module('plunker')
  .filter('custom', function () {
    return function(inputArray, args) {
      var outputArray = [];
      // Filtering logic below:
      angular.forEach(inputArray, function (item) {
        if (item.name.indexOf(args.viewValue) !== -1 || item.country.indexOf(args.viewValue) !== -1) {
          outputArray.push(item);
        }
      });
      return outputArray;
    };
  });

See the working plunker here: http://plnkr.co/edit/4s2ZxOokiXw9uMG5r75

You can find doc for creating custom filters here: https://docs.angularjs.org/guide/filter#creating-custom-filters

Upvotes: 3

Related Questions