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插件加載遠程數據。請有人告訴我,我該如何做到這一點?
其他選項可選擇? 我需要讓用戶可以選擇它,這樣我才能在後端獲得價值。 –