selec2 search - Return no results found message if a specific criteria didnt match

橙三吉。 提交于 2019-12-25 16:43:02

问题


I am using select2 4.0.3 for search drop down. As per my understanding its default functionality is not to match with the start of entries the drop down have. So I have implemented the below given code

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",
    matcher : function (params, data) {
        return matchStart(params, data);
    },
});

My problem is, the dropdown is not showing "No results found" message even if there is no matching results found. Can anyone help me on this.

Thanks in advance.


回答1:


Try changing the return value of matchStart from false to null.

Also you can remove the extra function around the matcher argument. The result:

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

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


来源:https://stackoverflow.com/questions/44383817/selec2-search-return-no-results-found-message-if-a-specific-criteria-didnt-mat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!