Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete widget in yii

I am trying to use the yii autocomplete build in widget. I have manage to show results from my users table intro the input filed with the following blocks of code:

public function actionSearch() 
{
    $res =array();
if (isset($_GET['term'])) 
    {           
        $qtxt ="SELECT user FROM tbl_user WHERE user LIKE :user";
        $command =Yii::app()->db->createCommand($qtxt);
        $command->bindValue(":user", '%'.$_GET['term'].'%', PDO::PARAM_STR);
        $res =$command->queryColumn();
    }
echo CJSON::encode($res);      
    Yii::app()->end();
}

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

As soon as a user is selected i want to redirect to that user page. I need to catch the user name in the select event. Or an alternative way is to catch both the user name and the user id to be able to easily redirect on that id.

like image 291
johnny j Avatar asked Oct 23 '22 14:10

johnny j


1 Answers

I hope this is a solution

'select' => 'js:function( event, ui ){
     // ui.item.id
     // ui.item.name
     top.location = "/user/view/?id=" + ui.item.id;
}'
like image 110
briiC Avatar answered Oct 26 '22 19:10

briiC