Custom matcher or native search per select input



I’m just wondering how to give the choice to user to use custom matcher (start with) or native search.

I know that custom matcher exist, what I don’t know is how to call the native way depending of an option.

I’m not sure what would be the best “user friendly” way to do it, any suggestion is welcome.

If anyone have an example, I will really appreciated it.

I have to implement everywhere in the application so I want to make sure I implement it correctly.

For me the native way is ok but some user prefer startwith way.



I don’t think you can ‘dynamically’ switch between the “built-in” matcher and a custom matcher. However, the built-in matcher is stored in $.fn.select2.defaults.matcher. Your custom matcher could call the built-in matcher if the user chooses that option.

Another approach would be to include the logic for both ways of searching (startsWith and substring) in your custom matcher. The only “special” logic I’m aware of in the built-in matcher is that it can match plain Latin letters in the search term against Latin letters with diacritic marks (accents, tildes, umlauts, etc.) in the Select2 data. If you wanted this functionality in your custom matcher you would have to write it yourself (although you could copy the functionality in the default matcher from the Select2 source code; it’s called stripDiacritics().


Thanks for the info
I will make some tests and get back to you