Tôi đang sử dụng các Select2 với AJAX (mã dưới đây):Set dữ liệu trong Select2 sau khi chèn với AJAX
$(".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
});
Vâng, nếu không tìm thấy khách hàng, người dùng có thể sử dụng một nút để mở một phương thức và thêm khách hàng mới, có thể sử dụng trả về (json với id và namae) của khách hàng mới và đặt dữ liệu (như tên) vào select2 như được chọn không?
$('.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;
});
Bạn có thể thực hiện công việc này không? – Sebastialonso