0
我想使用Select2插件来加载远程数据并自定义选择框。我遵循文档,它像一个魅力。但是,当用户找不到匹配项时,将显示消息:“未找到结果”。Select2 jquery插件中“找不到结果”动态显示默认选项
相反的消息我的要求是必须要显示的默认选项时,用户无法找到匹配,类似这样http://prntscr.com/dodf8k
function formatRepo (repo) {
if (repo.loading) return repo.text;
var markup = '<div class="clearfix">' +
'<div clas="col-sm-10">' +
'<div class="clearfix">' +
'<div class="col-sm-6"><b>' + repo.full_name + '</b></div>' +
'</div>';
if (repo.description) {
markup += '<div>' + repo.description + '</div>';
}
markup += '</div></div>';
return markup;
}
function formatRepoSelection (repo) {
return repo.full_name || repo.text;
}
$(document).ready(function(){
$(".js-data-example-ajax").select2({
ajax: {
url: "https://api.github.com/search/repositories",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return {
results: data.items
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
templateResult: formatRepo, // omitted for brevity, see the source of this page
templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});
});
我做了一个fiddle与选择2插件加载远程数据。请有人告诉我,我该如何做到这一点?
其他选项可选择? 我需要让用户可以选择它,这样我才能在后端获得价值。 –