0

I am using Angular ui-select (http://github.com/angular-ui/ui-select). The default behaviour of is to show all records containing the searched item, e.g. if the values contain:

ARMAGH ARGENTINA WARSAW

Typing 'AR' will find all of them. However, I only want it to match the beginning of the values, so in this case typing 'AR' should only find ARMAGH and ARGENTINA and not WARSAW. Is there a simple way to do this? Perhaps an option which I have missed?

1

1 Answer 1

2

You want to filter the result from left to right. So you need to add this filter (it is case-insensitive search)

app.filter('left2right', function() {
   return function( items, input) {
    var filtered = [];

   if(input === undefined || input === ''){
      return items;
    }

    angular.forEach(items, function(item) {

      if(item.name.toLowerCase().indexOf(input.toLowerCase()) == 0){
        filtered.push(item);
      }
    });

    return filtered;
  };
});

Then, you need to update the filter as follows

<ui-select-choices repeat="country in countries | left2right: $select.search">

instead of regular filter

<ui-select-choices repeat="country in countries | filter: $select.search">

Working demo: http://plnkr.co/edit/PeRNWR5J8wpydzr3MJVW?p=preview

Sign up to request clarification or add additional context in comments.

Comments

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.