jcv*_*gan 16 javascript ajax jquery jquery-select2 jquery-select2-4
我有以下代码(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);
}
});
Run Code Online (Sandbox Code Playgroud)
对于服务器端,我使用的是ASP MVC 4.使用ajax选择获取数据并呈现选项,但此选项不可选.阅读其他帖子,他们描述使用id函数,但是这个函数显然在select2的版本上消失了我正在使用2.4
我正在关注github "加载远程数据"上显示的文档中的ajax示例
web*_*ter 60
如果你的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
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8668 次 |
| 最近记录: |