简体   繁体   中英

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.

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;
}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM