Safira
Safira

Reputation: 275

Cannot autocomplete other textfields using CJuiAutoComplete

I have a model that has fields id_peg, nama_peg, and jabatan_peg. So here's what I wanna do:

  1. Autosuggestion that shows the list ofid_peg as I type in the text field id_peg
  2. When I select the suggested id_peg, the fields nama_peg and jabatan_peg are autocompleted based on that corresponding selected value (retrieved from database)

I've been trying to do the first one and I did it. But I'm stuck at the second one.

This is what I did:

view/melaporkan.php:

              $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
                    'name' => 'test1',
                    'source' => $this->createUrl('search'),
                    // additional javascript options for the autocomplete plugin
                    'options' => array(
                        'showAnim' => 'fold',
                        'select' => 'js:function(event, ui) {  
                              $('#nama_peg').val(ui.item.nama_peg);
                              $('#jabatan_peg').val(ui.item.jabatan_peg); 
                        }',
                    ),                            
                ));

controllers/sitecontroller.php

public function actionSearch() {
    $res = array(0 => array('id' => 1, 'value' => "id_peg"), 1 => array('id' => 2, 'value' => "jabatan_peg"), 2 => array('id' => 2, 'value' => "nama_peg"));
    if (isset($_GET['term'])) {
        $qtxt = "SELECT id_peg FROM pegawai WHERE id_peg LIKE :id_peg";
        $command = Yii::app()->db->createCommand($qtxt);
        $command->bindValue(":id_peg", '%' . $_GET['term'] . '%', PDO::PARAM_STR);
        $res = $command->queryColumn();

    }
    echo CJSON::encode($res);
    Yii::app()->end();
}

All it did was autosuggest for id_peg. When clicked some random id_peg, nama_peg and jabatan_peg were still empty.

What did I do wrong?

Upvotes: 0

Views: 173

Answers (1)

Prasenjit Chakroborty
Prasenjit Chakroborty

Reputation: 156

Actually the problem is controllers/sitecontroller.php page. You are sending only id_peg as JSON format but trying to get as ui.item.jabatan_peg.

It is always better to send your value as [{"id":"Caprimulgus europaeus","value":"European Nightjar"}] pattern and get the value ui.item.value at your select function.

Upvotes: 1

Related Questions