select2 使用 ajax 加载数据不能 select 任何选项
select2 load data using ajax cannot select any option
我有以下代码 (javascript):
$('#cbxConnections').select2({
minimumInputLength: 0,
multiple: false,
allowClear: true,
placeholder:{
text:"@Diccionario.Connections",
id:" @Diccionario.Connections"
},
ajax:{
url:'@Url.Action("GetActiveConnections","Admin")',
dataType: 'json',
type:'post',
data:function(params){
return {
q: params.term
};
},
processResults: function(data,page){
return {
results: data
};
}
},
escapeMarkup: function (markup) {
return markup;
},
templateResult: function(response){
return '<div>'+response.Name+'</div>';
},
templateSelection: function(response){
return response.Id;
},
id: function(connection){
console.log(connection);
}
});
对于服务器端,我使用 ASP MVC 4。
select 使用 ajax 获取数据并呈现选项,但此选项 select 不可用。
阅读其他帖子,他们描述了使用 id 函数,但是这个函数在 select2 的版本上似乎消失了我正在使用 2.4
我在 github 上显示的文档中遵循 ajax 的示例
"Loading remote data"
如果您的 ajax 响应没有 id 和 text 属性,您应该在客户端修复它们
这是 4.0 版的要求(不知道为什么)
ajax: {
processResults: function (data, params) {
params.page = params.page || 1;
// you should map the id and text attributes on version 4.0
var select2Data = $.map(data.result.data, function (obj) {
obj.id = obj._id.$id;
obj.text = obj.name;
return obj;
});
return {
results: select2Data,
pagination: {
more: data.result.more
}
};
}
}
我有以下代码 (javascript):
$('#cbxConnections').select2({
minimumInputLength: 0,
multiple: false,
allowClear: true,
placeholder:{
text:"@Diccionario.Connections",
id:" @Diccionario.Connections"
},
ajax:{
url:'@Url.Action("GetActiveConnections","Admin")',
dataType: 'json',
type:'post',
data:function(params){
return {
q: params.term
};
},
processResults: function(data,page){
return {
results: data
};
}
},
escapeMarkup: function (markup) {
return markup;
},
templateResult: function(response){
return '<div>'+response.Name+'</div>';
},
templateSelection: function(response){
return response.Id;
},
id: function(connection){
console.log(connection);
}
});
对于服务器端,我使用 ASP MVC 4。 select 使用 ajax 获取数据并呈现选项,但此选项 select 不可用。 阅读其他帖子,他们描述了使用 id 函数,但是这个函数在 select2 的版本上似乎消失了我正在使用 2.4
我在 github 上显示的文档中遵循 ajax 的示例 "Loading remote data"
如果您的 ajax 响应没有 id 和 text 属性,您应该在客户端修复它们
这是 4.0 版的要求(不知道为什么)
ajax: {
processResults: function (data, params) {
params.page = params.page || 1;
// you should map the id and text attributes on version 4.0
var select2Data = $.map(data.result.data, function (obj) {
obj.id = obj._id.$id;
obj.text = obj.name;
return obj;
});
return {
results: select2Data,
pagination: {
more: data.result.more
}
};
}
}