jQuery Select2 initSelection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15112988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select2 initSelection
提问by Lughino
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.
我在使用 ajax 调用设置方法 initSelection 时遇到问题,我返回“未定义”。我检查了一下,ajax 调用返回了正确的结果.. 我不明白如何设置回调方法以确保您选择“预设值”的方法。
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",
});
There is something wrong?
有什么问题吗?
采纳答案by Lughino
Here the way to solve:
解决方法如下:
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);
回答by vladCovaliov
Pay attention, the data you send to the callback function, needs to be an object, with an idand textattributes.
注意,你发送给回调函数的数据,需要是一个对象,有id和text属性。
This is what worked for me:
这对我有用:
initSelection: function(element, callback) {
var id;
id = $(element).val();
if (id !== "") {
return $.ajax({
url: url,
type: "POST",
dataType: "json",
data: {
id: id
}
}).done(function(data) {
var results;
results = [];
results.push({
id: data.id,
text: data.name
});
callback(results[0]);
});
}
}