使用 CJuiAutoComplete 搜索两个字段值

问题描述 投票:0回答:1

我想使用cJuiautocomplete搜索两个字段,即名称和标签,如何执行此任务??

我的CJuiAutoComplete代码:

<?php   $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
    'name'=>'test1',
    'model'=>$model,
    'attribute'=>'name',
    //'attribute'=>'tags',
    'value'=>'',
    'source'=>$this->createUrl('campaign/autocompleteTest'),
    'options'=>array(
            'showAnim'=>'fold',
            'select'=>' function(event,ui) { this.value=ui.item.value; ',
    ),
    'htmlOptions'=>array( 'class' => 'input-xx search-query'),
   ));?>

我的控制器代码:

public function actionAutocompleteTest() {
    $res =array();
    $res2 =array();
    if (isset($_GET['term'])) {

        $qtxt ="SELECT name FROM crm_campaign WHERE name LIKE :name";
        $command =Yii::app()->db->createCommand($qtxt);
        $command->bindValue(":name", '%'.$_GET['term'].'%', PDO::PARAM_STR);
        $res =$command->queryColumn();
        $tagsql ="SELECT tags FROM crm_campaign WHERE tags LIKE :name";
        $tagcommand =Yii::app()->db->createCommand($tagsql);
        $tagcommand->bindValue(":name", '%'.$_GET['term'].'%', PDO::PARAM_STR);
        $tag =$tagcommand->queryColumn();
        foreach ($tag as $t)
        {
            $res2 = explode(',',$t);
        }

    }

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

任何人都可以让我走上正确的道路吗?

php autocomplete yii
1个回答
0
投票

你可以使用js选项:

'source'=>'js: function(request, response) {
    $.ajax({
        url: "'.$this->createUrl('campaign/autocompleteTest').'",
        dataType: "json",
        data: {
            term: request.term,
            tag: $("#tag_id").val()
        },
        success: function (data) {
                response(data);
        }
    })
 }',

编辑:

foreach ($tag as $t)
        {
            $res2 = explode(',',$t);
            $res = array_merge($res,$res2);
        }
© www.soinside.com 2019 - 2024. All rights reserved.