Select2 initSelection [英] Select2 initSelection
本文介绍了Select2 initSelection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在用ajax调用设置方法initSelection时遇到问题,我返回"undefined". 我检查了一下,ajax调用返回了正确的结果.. 我不了解该方法如何设置回调方法,以确保选择预设值".
I have a problem to set the method initSelection with an ajax call, I returns "undefined". I checked and the ajax call returns the correct result .. I donot understand the method how to set the callback method to make sure that you select 'the preset value.
function mediaFormatResult(media) {
var markup = "<div class='media-title'>" + media.name + ", " + media.prov + " (" + media.region + ")</div>";
return markup;
}
function mediaFormatSelection(media) {
return media.name +", " + media.prov + " (" + media.region + ")";
}
$field = $('#comune');
$($field).select2({
placeholder: "Seleziona il tuo comune",
minimumInputLength: 3,
initSelection: function(element, callback) {
return $.ajax({
type: "POST",
url: "myurl",
dataType: 'json',
data: { id: (element.val())},
success: function(data){
//results: data.results;
}
}).done(function(data) {
//console.log(data);
callback(data);
});
},
ajax: {
quietMillis: 100,
url: "myurl",
dataType: 'json',
type: 'POST',
data: function (term, page) {
return {
q: term,
page_limit: 10
};
},
results: function (data, page) {
return {results: data.results};
}
},
formatResult: mediaFormatResult,
formatSelection: mediaFormatSelection,
formatNoMatches: function () { return "Nessun risultato trovato!";},
formatSearching: function () { return "Ricerco.."; },
formatInputTooShort: function(input, min) {return "Inserire "+ (min - input.length) + " caratteri.";},
dropdownCssClass: "bigdrop",
});
有什么问题吗?
推荐答案
以下是解决方法:
initSelection: function(element, callback) {
return $.ajax({
type: "POST",
url: path,
dataType: 'json',
data: { id: (element.val())},
success: function(data){
}
})
必须注意要在控制器中创建数组,并且应该像这样:
care must be taken that the array is created in the controller and it should be something like this:
foreach ($entities as $member) {
$json['id'] = $member->getId();
$json['name'] = $member->getComune();
$json['region'] = $member->getRegione()->getRegione();
$json['prov'] = $member->getProvincia()->getSigla();
}
$result = $json;
return json_encode($result);
这篇关于Select2 initSelection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文