Skip to content Skip to sidebar Skip to footer

Select2 - Searching Wildcard Matches

I am using Select2 to style my select box and add functionality and I am wondering how I can make the search rules more lenient. Currently if I have 'New Mexico' in the dropdown, I

Solution 1:

Alright, apparently you can set custom matchers to accomplish this. I found this one and it seems to be working as desired:

$(function () { 
    $('.states').select2({
        matcher: function (params, data) {
            if ($.trim(params.term) === '') {
                return data;
            }

            keywords=(params.term).split(" ");

            for (var i = 0; i < keywords.length; i++) {
                if (((data.text).toUpperCase()).indexOf((keywords[i]).toUpperCase()) == -1) 
                returnnull;
            }
            return data;
        }
    });
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script><linkhref="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" /><selectclass="states"style="width: 50%"><optionvalue="NH">New Hampshire</option><optionvalue="NJ">New Jersey</option><optionvalue="NM">New Mexico</option><optionvalue="TB">Tshirt, Black</option></select>

Post a Comment for "Select2 - Searching Wildcard Matches"