0

I am using Angular UI-Bootstrap's typeahead

<input type="text" name="recipient" id="recipient" placeholder="Friend's name or username"
 autofocus="autofocus"
 class="form-control"
 ng-model="payment.recipient"
 typeahead="friend.username for friend in friends | filter:$viewValue | limitTo:4"
 typeahead-template-url="typeaheadTemplate.html"
 typeahead-editable="false"
 ng-required="true"/>

Each Friend object has the following structure:

{
    name: friendship.name,
    username: friendship.username,
    picture: friendship.picture // Url
}

It doesn't make sense to allow users to search by picture (it's a url to the avatar) and have had no success modifying the filter.

What I've tried:

typeahead="friend.username for friend in friends| filter:{username:username, name:name} | filter:$viewValue | limitTo:4"
typeahead="friend.username for friend in friends | filter:{username:$viewValue, name:$viewValue} | limitTo:4"

Using $viewValue twice on the filter expression seems to break it... Any ideas?

Iñigo Beitia
  • 6,303
  • 4
  • 40
  • 46
  • Is this a translation typo or does your code actually look like: "friend.username for friend for friend" – Rob J Apr 25 '14 at 22:25
  • @RobJacobs that was a a translation typo. Fixed it. – Iñigo Beitia Apr 26 '14 at 09:01
  • It seems to be doing an 'and' filter ($viewValue matches on name and username) rather than an 'or' filter. – Rob J Apr 26 '14 at 12:12
  • See plunk here: http://plnkr.co/edit/EOmc7SWJaukcpMsKKKgQ?p=preview based on question here: http://stackoverflow.com/questions/16045069/limit-angular-ui-bootstrap-typeahead-to-specific-object-property – Rob J Apr 26 '14 at 12:38

1 Answers1

0

Try this:

typeahead="friend.username for friend in myFilterFunc($viewValue) | limitTo:4"

and in your controller:

$scope.myFilterFunc = function(viewValue) {
  var filteredFriends = [];
  angular.forEach($scope.friends, function(friend) {
    if ((friend.username.indexOf(viewValue) > -1) || 
       (friend.name.indexOf(viewValue) > -1)) 
       {
         filteredFriends.push(friend);
       }
  });
  return filteredFriends;
};
Wawy
  • 6,259
  • 2
  • 23
  • 23