插入使用AJAX后设置在选择二数据 [英] Set data in Select2 after insert with AJAX
本文介绍了插入使用AJAX后设置在选择二数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是选择二用AJAX(的code以下):
$(选择2 - AJAX)。选择二({
占位符:搜索用户,
minimumInputLength:1,
AJAX:{
网址:$('#网址搜索客户端)VAL()。
数据类型:JSON,
类型:'后',
数据:功能(术语,页){
返回 {
过滤:长期
};
},
结果:功能(数据页){
返程{结果:数据};
}
},
宽度:'50%',
formatInputTooShort:函数(){return'的Informe MAIS caracteres; },
formatResult:formatResultSelectAjax,//不再赘述,请参阅本页面的源代码
formatSelection:formatSelectAjaxValue,//不再赘述,请参阅本页面的源代码
dropdownCssClass:bigdrop//应用CSS,使下拉高
});
好了,如果没有找到客户端,用户可以使用一个按钮来打开一个模式,并添加新的客户端,可以用返回(JSON通过ID和namae)新客户,并把数据(如姓名)到作为选择的选择2?
$('。BTN-形式的客户端)。点击(函数(){
$阿贾克斯({
网址:$('#FRM客户端)ATTR(行动),
数据类型:JSON,
类型:'后',
数据:$('#FRM客户端)序列化()
})。完成(功能(数据){
$('#模式 - 客户端)。模式(隐藏)
});
返回false;
});
解决方案
我设法使它工作。帖子中的jQuery后,我得到的新数据的JSON,并设置隐藏的输入和选择(.select2阿贾克斯')
$('#客户端= ID)VAL(data.id)。
$('#模式,solicitante)模式(隐藏)。 //这是我的
$(选择2 - AJAX)选择2('数据',{ID:data.id,名称:data.name,电子邮件:data.email});
i'm using the Select2 with AJAX (the code below):
$(".select2-ajax").select2({
placeholder: "Search user",
minimumInputLength: 1,
ajax: {
url: $('#url-search-client').val(),
dataType: 'json',
type: 'post',
data: function (term, page) {
return {
filter: term
};
},
results: function (data, page) {
return {results: data};
}
},
width : '50%',
formatInputTooShort: function () {return 'Informe mais caracteres'; },
formatResult: formatResultSelectAjax, // omitted for brevity, see the source of this page
formatSelection: formatSelectAjaxValue, // omitted for brevity, see the source of this page
dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller
});
Well, if not found client, the user can be use a button to open a modal and add the new client, is possible use the return(json with id and namae) of new client and put the data (like name) into the select2 as selected?
$('.btn-form-client').click(function() {
$.ajax({
url: $('#frm-client').attr('action'),
dataType: 'json',
type: 'post',
data: $('#frm-client').serialize()
}).done(function (data) {
$('#modal-client').modal('hide')
});
return false;
});
解决方案
I managed to make it work. After POST in jQuery, i get a JSON with the new data and set the hidden input and the select ('.select2-ajax')
$('#client=id').val(data.id);
$('#modal-solicitante').modal('hide'); //This is my
$(".select2-ajax").select2('data', {id: data.id, name: data.name, email: data.email});
这篇关于插入使用AJAX后设置在选择二数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文