select2 ajax selected value
//YOUR AJAX SELECT2
$("#myselect2").select2({
dropdownParent: $('#test_modal'),
width: '100%',
placeholder: 'Seach',
ajax: {
url: "<?=base_url('dashboard/api/v1/dropdown')?>",
dataType: 'json',
delay: 250,
processResults: function(data) {
return {
results: data
};
},
cache: true
}
});
// TRIGGER EDIT AND SET THE VALUE FOR SPECIFIC MODAL / FORM
$(document).on('click', '.edit_button', function() {
var something_id = $('#something_id').val();
$.ajax({
url: "<?php echo base_url('dashboard/api/v1/page/edit');?>" + "/" + something_id,
method: 'POST',
dataType: 'json',
success: function(data) {
if (data.id) {
// Set selected
var $newOption = $("<option selected='selected'></option>").val(data.id).text(data.text);
$("#myselect2").append($newOption).trigger('change');
} else {
$('#myselect2').val(null).trigger('change');
}
}
});
});