0

我正在尝试在我的项目中实现 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,
                    )) ?>

地址控制器.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";
    }
4

3 回答 3

0

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

于 2014-02-28T05:47:57.317 回答
0

啊,jui 的自动完成功能需要以下格式的数据:

预期数据格式

来自本地数据、url 或回调的数据可以有两种变体:

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

因此,在您的操作中,您必须返回 Json :

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

编辑:CJSON 文档

于 2012-05-25T17:46:03.207 回答
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

             ));
    ?>

它的工作正常...

于 2012-05-25T18:50:30.697 回答