Answer the question
In order to leave comments, you need to log in
How to force Select2 to search in data attributes?
I'm using the latest version 4.0.12 of the Select2 script.
My dropdown looks like this:
<select id="langselect">
<option data-lang="Английский">English</option>
<option data-lang="Русский">Russian</option>
</select>
matcher
to search by first letter:function matchStart(term, text) {
if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
return true;
}
return false;
}
$.fn.select2.amd.require(['select2/compat/matcher'], function(oldMatcher) {
$("#langselect").select2({
matcher: oldMatcher(matchStart),
});
});
matchStart()
to be able to search on data-* attributes as well. I am trying to write code like this:if ($(data.element).data('lang').toUpperCase().indexOf(term.toUpperCase()) == 0) {
return true;
}
Cannot read property 'indexOf' of undefined
, toString() does not help. I suppose the code should look different, but I don't know how. Please help fix the error.
Answer the question
In order to leave comments, you need to log in
https://select2.org/configuration/data-attributes
Changed:
Where did you get the data from ? Add to the list of function arguments
function matchStart( data , term, text) { ... }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question