Ho risolto con questo codice :
codice:
// Initialize select2
$("#id_id_comuni").select2({
theme: "bootstrap-5",
ajax: {
url: "<?= base_url('AjaxRequest/getComuniSelect')?>",
type: "post",
dataType: 'json',
delay: 250,
data: function (params) {
// CSRF Hash
var csrfName = $('.txt_csrfname').attr('name'); // CSRF Token name
var csrfHash = $('.txt_csrfname').val(); // CSRF hash
return {
searchTerm: params.term, // search term
[csrfName]: csrfHash // CSRF Token
};
},
processResults: function (response) {
// Update CSRF Token
$('.txt_csrfname').val(response.token);
return {
results: response.data
};
},
cache: true
}
});
data lato server arriva così :
codice:
foreach($lista_comuni as $comune){
$data[] = array(
"id" => $comune->id,
"text" => $comune->cap.' '.$comune->localita,
);
}
Le option hanno bisogno di 2 valori id (che diventa il value della option) e text (che diventa il testo che si vede premendo la select)