Frage

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

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top