Rallina
Rallina

Reputation: 23

Yii CJuiAutoComplete data from Ajax request

I have a task form for an application I am building which allows me to specify what contact it is assigned to. I had set up a dependent dropdown in Yii with the following code:

echo $form->dropDownList($model,'associationType',
array('none'=>'None','contact'=>'Contact','sale'=>'Sale','account'=>'Account',
'project'=>'Project','case'=>'Case'),
                    array(
                        'ajax' => array(
                            'type'=>'POST', //request type
                            'url'=>CController::createUrl('tasks/parseType'), //url to call.
                            //Style: CController::createUrl('currentController/methodToCall')
                            'update'=>'#auto_complete', //selector to update
                            )
                    ));

What I'm trying to do is use the CJuiAutoComplete widget with the dropDown specifying which array to grab. So the drop down is selected to contacts, it should get a list of contacts etc.

Here is what I have for the CJui widget

$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
                'name'=>'auto_select',
                'source' => $names,
                'options'=>array(
                    'minLength'=>'2',
                    'select'=>'js:function( event, ui ) {
                        $("#'.CHtml::activeId($model,'associationId').'").val(ui.item.id);
                        return false;
                    }',
                ),
            ));

The variable $names is just a placeholder for now, but in my controller method I pass it a JSON encoded array with id and name. Controller code:

public function actionParseType() {
            //if(isset($_POST['TaskChild']['associationType'])){
                //$type=$_POST['TaskChild']['associationType'];
                $type='sale';
                $sql = 'SELECT id, name FROM x2_'.$type.'s';
                $cmd = Yii::app()->db->createCommand($sql);
                $res = $cmd->queryAll();

                echo CJSON::encode($res);

            //}
}

Right now I'm forcing it to use "Sale" but I don't get anything when I call the method, and was wondering how I might go about fixing this. I'm still a little new to Yii so I've been mostly reading wiki/forum posts on how these kinds of thing are done. Any help is greatly appreciated, thanks!

Upvotes: 2

Views: 6184

Answers (1)

ldg
ldg

Reputation: 9402

Try something like this in your controller action:

    $sql = 'SELECT people_id as id, CONCAT(first_name," ",last_name) as value, first_name as label FROM people WHERE first_name LIKE :qterm ORDER BY first_name ASC';
    $command = Yii::app()->db->createCommand($sql);
    $qterm = $_GET['term'].'%';
    $command->bindParam(":qterm", $qterm, PDO::PARAM_STR);
    $result = $command->queryAll();
    echo CJSON::encode($result); exit;

Then you can check it by using this in your widget 'options' array: 'select'=>'js:function(event, ui) { console.log(ui.item.id +":"+ui.item.value); }'

Upvotes: 2

Related Questions