So it worked totally fine if I only used one CJuiAutoComplete. But it doesn't since I added another one(from different tables). The original view was as follows:
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'myIdPeg',
//'value' => $model->id_peg,
'sourceUrl' => $this->createUrl('search'),
// additional javascript options for the autocomplete plugin
'options' => array(
'minLength' => '1',
'showAnim' => 'fold',
'focus' => 'js:function( event, ui ) {
$( "#myIdPeg" ).val( ui.item.id_peg );
return false;
}',
'select' => 'js:function(event, ui) {
$("#myIdPeg").val(ui.item.id_peg);
$("#myNamaPeg").val(ui.item.nama_peg);
$("#myJabatanPeg").val(ui.item.jabatan_peg);
return false;
}',
),
'htmlOptions' => array(
'style' => 'width: 98%',
'placeholder' => 'NIP Baru'
),
));
Yii::app()->clientScript->registerScript('input', '
$("#myIdPeg").data("autocomplete")._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>"+item.id_peg + " - " + item.nama_peg+"</a>")
.appendTo( ul );
};');
?>
The Controller:
public function actionSearch() {
$res = array();
if (isset($_GET['term'])) {
$qtxt = "SELECT
DISTINCT
bc.id_peg as id_peg,
bc.nama_peg as nama_peg,
bc.jabatan_peg as jabatan_peg,
CONCAT (bc.id_peg, ' - ', bc.nama_peg) as id_nama_peg
FROM pegawai bc
WHERE bc.id_peg LIKE :qterm
ORDER BY bc.id_peg, bc.nama_peg ASC";
$command = Yii::app()->db->createCommand($qtxt);
$command->bindValue(":qterm", '%' . $_GET['term'] . '%', PDO::PARAM_STR);
//$res = $command->queryColumn();
$res = $command->queryAll();
}
echo CJSON::encode($res);
Yii::app()->end();
}
But, when I added this to the view:
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'mySubKegiatan',
'sourceUrl' => $this->createUrl('searchkeg'),
'options' => array(
'minLength' => '1',
'showAnim' => 'fold',
'focus' => 'js:function( event, ui ) {
$( "#mySubKegiatan" ).val( ui.item.sub_kegiatan );
return false;
}',
'select' => 'js:function(event, ui) {
$("#mySubKegiatan").val(ui.item.sub_kegiatan);
$("#myBidang").val(ui.item.bidang);
$("#mySeksi").val(ui.item.seksi);
$("#myKegiatan").val(ui.item.kegiatan);
return false;
}',
),
'htmlOptions' => array(
'style' => 'width: 98%',
'placeholder' => 'Nama Sub Kegiatan'
),
));
Yii::app()->clientScript->registerScript('input', '
$("#mySubKegiatan").data("autocomplete")._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>"+item.sub_kegiatan + " - " + item.kegiatan+"</a>")
.appendTo( ul );
};');
?>
And this to the Controller:
public function actionSearchkeg() {
$res = array();
if (isset($_GET['term'])) {
$qtxt = "SELECT
DISTINCT
bc.kode_master as kode_master,
bc.kegiatan as kegiatan,
bc.sub_kegiatan as sub_kegiatan,
bc.bidang as bidang,
bc.seksi as seksi,
CONCAT (bc.sub_kegiatan, ' - ', bc.kegiatan) as sub_nama_keg
FROM kegiatan bc
WHERE bc.sub_kegiatan LIKE :qterm
ORDER BY bc.kegiatan, bc.kode_master ASC";
$command = Yii::app()->db->createCommand($qtxt);
$command->bindValue(":qterm", '%' . $_GET['term'] . '%', PDO::PARAM_STR);
//$res = $command->queryColumn();
$res = $command->queryAll();
}
echo CJSON::encode($res);
Yii::app()->end();
}
Neither of them works. I tried to track the problem by deleting the first CJuiAutoComplete. Turned out the second one that I added later worked.
So the problem is that they work fine, separately. But they don't when put together. Why is that? What have I missed? Thank you. I'm pretty sure I have set different unique parameters as it said here. Really hoping for some help here. Thanks.