Contekan Codeigniter Select2 Ajax
Berikut ini merupakan contekan source code Select2 dengan Ajax PHP
CSS dan JS Select2
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css"
rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
*Butuh Jquery juga
Script AJAX
$(document).ready(function() {
$("#nim").select2({
placeholder: 'Pilih Data',
minimumInputLength: 4,
ajax: {
url: "<?=base_url()?>datauser",
dataType: 'json',
delay: 250,
data: function (data) {
return {
searchTerm: data.term // search term
};
},
processResults: function (response) {
return {
results:response
};
},
cache: true
}
});
});
Script PHP (CodeIgniter)
public function datauser() {
$this->load->model("Model");
$mhs = $this->Model->listUserByParam($this->input->get('searchTerm'))->result_array();
foreach($mhs as $user){
$response[] = array(
"id" => $user['id_user'],
"text" => $user['nama_user']
);
}
echo json_encode($response);
}
Script HTML
<select class="form-control input-sm" id="nim" required name="nim"></select>
Komentar
Posting Komentar