jquerypluginsjquery-select2

Select2 search options


I downloaded Select2 jquery plug in and have it all set up - I have a basic dropdown here.

<SELECT NAME="GENDER">  
 <OPTION VALUE="1">MALE
 <OPTION VALUE="2">FEMALE  
 <OPTION VALUE="3">UNDEFINED
</SELECT>

I applied the plug in and it works - not a problem.

I've been reviewing the select2 documentation and what I'm trying to do is instead of searching by gender such as typing Female and Male, etc - I want the user to simply press 1 thus it brings up Male, 2 for Female.

Has anyone attempted this in select2?


Solution

  • Have a look at the "matcher" option in the documentation: http://ivaynberg.github.io/select2/#documentation

    Override the matcher function to check against both the text() and the val() of the given option. Untested example:

    $('select').select2({
      matcher: function(term, text, option) {
        return text.toUpperCase().indexOf(term.toUpperCase())>=0 || option.val().toUpperCase().indexOf(term.toUpperCase())>=0;
      }
    });