Widget auto-complete in yii

I am trying to use yii autocomplete assembly in widgets. I managed to show the results from the table of my users by entering the input 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 ) {
                //
            }'
),
));

Once the user is selected, I want to redirect the user to this page. I need to catch the username in the select event. Or an alternative way is to catch the username and user ID in order to be able to easily redirect this identifier.

+5
source share
1 answer

Hope this is a solution.

'select' => 'js:function( event, ui ){
     // ui.item.id
     // ui.item.name
     top.location = "/user/view/?id=" + ui.item.id;
}'
+3
source

All Articles