Reputation: 183
With a standard select2 dropdown box, populated with a list of names from a database call, is there a way to search on hidden items within the search area?
Example:
Select2 box shows to end user "Charlie Watts" but actually the options value holds "Charlie Watts (22)". I want the use to be able to search for 22, but not show it by default to the end user.
TIA
Upvotes: 3
Views: 1803
Reputation: 488
Yep, you can achieve that using the formatResult
and/or formatSelection
methods. There's a great example of using them in the Select2 Docs: Templating.
In your format
function, filter out the " (22)" part of your value and return everything before it.
On a UX note, it could be strange to see matches appear that don't give any indication as to why they match. If that doesn't matter for your use-case, carry on.
Upvotes: 3