Jquery select2 refresh data

I use select2to select and ajaxload data, how can I replace the old data when the state was changed after the initial selection.

My download code:

$('.datetimepicker').on("changeDate", function() {  
        //refresh selectbox ??
        doctor_id = $(".select2-doctors").val();
        clinic_id = $(".select2-clinics").val();
        date = $('.datetimepicker').datepicker('getFormattedDate');
        $.ajax({
            url: '/admin/appointments/time_slots',
            type: 'GET',
            dataType: 'json',
            data: {doctor_id: doctor_id, clinic_id: clinic_id, date: date}
        })
        .done(function(data) {
            console.log("success");
            console.log(data);
            $('.select2-slot').select2({
                data: data.slots
            });
        })
        .always(function() {
            console.log("complete");
        });
    });
+4
source share
2 answers

Try to clear the select2 options before updating the data, because by default it adds data to existing parameters.

$('.select2-slot').empty();
$('.select2-slot').select2({
    data: data.slots
});
+9
source
instance && $('.select2').empty();
var instance = $('.select2').select2({
    data: data
});
+1
source

Source: https://habr.com/ru/post/1625616/


All Articles