这是我现在的控制器:
public function tambah()
{
$this->data['namaProvinsi'] = $this->registrasi_model->get_nama_provinsi();
$this->data['main_view'] = 'program/administrasi/registrasi_form';
$this->data['form_action'] = site_url('program/administrasi/registrasi/tambah');
if (! $_POST) {
$registrasi = (object) $this->registrasi_model->default_value;
} else {
$registrasi = $this->input->post(null, true);
}
if (! $this->registrasi_model->validate('form_rules')) {
$this->data['values'] = (object) $registrasi;
$this->load->view($this->layout, $this->data);
return;
}
}
模型:
public function get_nama_provinsi()
{
$query = $this->db->query('SELECT name FROM provinces ORDER BY name ASC');
$dropdowns = $query->result();
if(! $dropdowns){
$finaldropdown[''] = " - Pilih - ";
return $finaldropdown;
}
else{
foreach ($dropdowns as $dropdown){
$dropdownlist[$dropdown->name] = $dropdown->name;
}
$finaldropdown = $dropdownlist;
$finaldropdown[''] = " - Pilih - ";
return $finaldropdown;
}
}
视图:
<div class="form-group form-group-sm<?php set_validation_style('Provinsi')?>">
<?php echo form_label('Provinsi', 'provinsi', array('class' => 'control-label col-sm-2')) ?>
<div class="col-sm-3">
<?php
$atribut_prov = 'class="form-control"';
$selectedProv = $this->registrasi_model->format_idPJTKI_namaPJTKI($values->ID_PJTKI);
echo form_dropdown('Provinsi', $namaProvinsi, $selectedProv, $atribut_prov);
?>
</div>
<?php if (form_error('Provinsi')) : ?>
<div class="col-sm-9 col-sm-offset-3">
<?php echo form_error('Provinsi', '<span class="help-block">', '</span>');?>
</div>
<?php endif ?>
</div>
我希望你理解我的观点,我想使用文本框,如果我输入内容,它将显示数据库中最接近的值。
我试图在控制器函数上做json_encode,但我不知道接下来要做什么,我不确定是否使用json_encode将数据从控制器传递到js脚本是正确的。
答案 0 :(得分:0)
感谢aman,我在这种情况下使用了select2,这就是我的所作所为:
将此添加到视图中:
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
并改变
$atribut_prov = 'class="form-control"';
到
$atribut_Prov = 'class="form-control dropProv"';
echo form_dropdown('Provinsi', $namaProvinsi, $values->Provinsi, $atribut_Prov);
最后添加这个js:
<script>
$(document).ready(function () {
$(".dropProv").select2({
placeholder: "Pilih Provinsi"
});
});
</script>