简体   繁体   中英

By using a matcher function in Select2 plugin, no results found message is not appearing

In normal the the no results found message shows when unrecognised name enters, but by adding the below code to filter results to show only the first letter matches there is no message shown.

 function matchStart(params, data) {
    params.term = params.term || '';
    if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) == 0) {
        return data;
    }
    return false;
}

$("select").select2({
    placeholder: "Input country name or select region",

    noResults: function () {
        return"No results found"
    },
    matcher: function (params, data) {
        return matchStart(params, data);
    },
});

Know this is an old one but I managed to find an answer here: selec2 search - Return no results found message if a specific criteria didnt match

So credit to user ikkuh for the answer. My issue was with returning false in the matcher function, try returning null instead - worked for me!

You need replace "return false;" to "return null;" in function matchStart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM