Yii CJuiAutoComplete不工作

时间:2012-05-25 14:22:02

标签: jquery yii

我试图在我的项目中实现CJuiAutoComplete,但它无法正常工作。我过去几天研究了这个问题,并尝试了一切。似乎正在发生的事情(或在这种情况下不会发生)是控制器中的查找操作未被调用。如果我将源设置为一个简单的项目数组,我也无法使它工作。我做错了什么?

_form.php这个

            <?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
                        'model' => $model,
                        'attribute' => 'zipcode',
                        'source' => $this->createUrl('address/lookup'),
                        'name' => 'zipcode',
                        'htmlOptions' => array('size'=>'5'),
                        'options' => array(
                            'showAnim'=>'fold',
                            'minLength' => 1,
                    )) ?>

AddressController.php

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('index','view'),
            'users'=>array('*'),
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('create','update','lookup'),
            'users'=>array('@'),
        ),
        array('allow', // allow admin user to perform 'admin' and 'delete' actions
            'actions'=>array('admin','delete'),
            'users'=>array('admin'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
        ),
    );
}


    public function actionLookup()
    {
        echo "Lookup Action";
    }

3 个答案:

答案 0 :(得分:0)

啊,jui's autocomplete期望数据采用以下格式:

  

预期的数据格式

     

来自本地数据,网址或回调的数据可以有两种变体:

     

字符串数组:   [&#34; Choice1&#34;,&#34; Choice2&#34; ]
  具有标签和值属性的对象数组:   [{label:&#34; Choice1&#34;,value:&#34; value1&#34; },...]

所以在你的行动中,你必须回归Json:

echo CJSON::encode(array("Look up action"));

修改:CJSON docs

答案 1 :(得分:0)

试试这个对你来说非常简单..

public function actionAutoCompleteLookup()
        {
           if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
           {

              $name = $_GET['q']; 

               $qtxt ="SELECT name FROM address WHERE name LIKE '%".$name."%'";
               $command =Yii::app()->db->createCommand($qtxt);

               $userArray =$command->queryColumn();

              $returnVal = '';
              foreach($userArray as $userAccount)
              {
                 $returnVal .= $userAccount->getAttribute('first_name').'|'
                                             .$userAccount->getAttribute('user_id')."\n";
              }
              echo $returnVal;
           }
        }

在这样的视图代码中..

<?php $this->widget('CAutoComplete',
          array(
                         //name of the html field that will be generated
             'name'=>'name', 
                       //replace controller/action with real ids
             'url'=>array('address/AutoCompleteLookup'), 
             'max'=>10, //specifies the max number of items to display

                         //specifies the number of chars that must be entered 
                         //before autocomplete initiates a lookup

             ));
    ?>

工作正常......

答案 2 :(得分:0)

It Jquery问题请禁用或删除jquery-min.js文件,然后检查其工作正常。