0
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'test1',
'value'=>'test21',
'source'=>$this->createUrl('jui/autocompleteTest'),
// additional javascript options for the autocomplete plugin
'options'=>array(
        'showAnim'=>'fold',
),
));

我是 Yii 框架的新手。

我正在研究自动完成但无法理解上面给出的代码,createUrl 是什么意思?

我们需要按用户创建页面还是该行本身创建页面?

请帮助我。

这是完整的参考代码

$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
//'model'=>$model,
//'attribute'=>'name',
'id'=>'country-chain',
'name'=>'country_chain',
'source'=>$this->createUrl('request/suggestCountry'),
'options'=>array(
    'delay'=>300,
    'minLength'=>2,
    'showAnim'=>'fold',
    'select'=>"js:function(event, ui) {
        $('#label').val(ui.item.label);
        $('#code').val(ui.item.code);
        $('#call_code').val(ui.item.call_code);
    }"
),
'htmlOptions'=>array(
    'size'=>'40'
),
));

来源:以上代码 Live Example

我需要知道的是在哪里添加模型名称以及在哪里添加数据库字段名称


我的型号名称是

详细信息 ,在此字段是:

  • 身份证、姓名、邮箱、手机

我想要自动完成下的名称 - 电子邮件和手机应该像上面的例子一样

提前
致谢

在此处输入图像描述

对不起朋友,

这是结构

型号名称:详情

控制器名称是 - DetailsController

在视图 > 详细信息 > _form.php

    <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'details-form',
'enableAjaxValidation'=>false,)); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>

<div class="row">
    <?php echo $form->labelEx($model,'id'); ?>


    <?php echo $form->textField($model,'id'); ?>
    <?php echo $form->error($model,'id'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'name'); ?>
    <?php $this->widget('CAutoComplete',
      array(
                     //name of the html field that will be generated
         'name'=>'name', 
                   //replace controller/action with real ids
         'url'=>array('DetailsController/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

         ));    ?>
            <?php echo $form->error($model,'name'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'phone'); ?>
    <?php echo $form->textField($model,'phone'); ?>
    <?php echo $form->error($model,'phone'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'email'); ?>
    <?php echo $form->textField($model,'email'); ?>
    <?php echo $form->error($model,'email'); ?>
</div>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div><?php $this->endWidget(); ?> </div><!-- form -->

控制器代码是

<?php  class DetailsController extends Controller { /**
 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
 * using two-column layout. See 'protected/views/layouts/column2.php'.
 */
public $layout='//layouts/column2';

/**
 * @return array action filters
 */
public function filters()
{
    return array(
        'accessControl', // perform access control for CRUD operations
    );
}

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

          $name = $_GET['q']; 

          $criteria = new CDbCriteria;
          $criteria->condition = "name LIKE :sterm";
          $criteria->params = array(":sterm"=>"%$name%");

          $userArray = User::model()->findAll($criteria);
          $returnVal = '';
          foreach($userArray as $userAccount)
          {
             $returnVal .= $userAccount->getAttribute('name').'|'
                                         .$userAccount->getAttribute('id')."\n";
          }
          echo $returnVal;
       }
    }

/**
 * Specifies the access control rules.
 * This method is used by the 'accessControl' filter.
 * @return array access control rules
 */
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'),
            '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('*'),
        ),
    );
}

/**
 * Displays a particular model.
 * @param integer $id the ID of the model to be displayed
 */
public function actionView($id)
{
    $this->render('view',array(
        'model'=>$this->loadModel($id),
    ));
}

/**
 * Creates a new model.
 * If creation is successful, the browser will be redirected to the 'view' page.
 */
public function actionCreate()
{
    $model=new Details;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Details']))
    {
        $model->attributes=$_POST['Details'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->id1));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

/**
 * Updates a particular model.
 * If update is successful, the browser will be redirected to the 'view' page.
 * @param integer $id the ID of the model to be updated
 */
public function actionUpdate($id)
{
    $model=$this->loadModel($id);

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Details']))
    {
        $model->attributes=$_POST['Details'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->id1));
    }

    $this->render('update',array(
        'model'=>$model,
    ));
}

/**
 * Deletes a particular model.
 * If deletion is successful, the browser will be redirected to the 'admin' page.
 * @param integer $id the ID of the model to be deleted
 */
public function actionDelete($id)
{
    if(Yii::app()->request->isPostRequest)
    {
        // we only allow deletion via POST request
        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }
    else
        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}

/**
 * Lists all models.
 */
public function actionIndex()
{
    $dataProvider=new CActiveDataProvider('Details');
    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));
}

/**
 * Manages all models.
 */
public function actionAdmin()
{
    $model=new Details('search');
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['Details']))
        $model->attributes=$_GET['Details'];

    $this->render('admin',array(
        'model'=>$model,
    ));
}

/**
 * Returns the data model based on the primary key given in the GET variable.
 * If the data model is not found, an HTTP exception will be raised.
 * @param integer the ID of the model to be loaded
 */
public function loadModel($id)
{
    $model=Details::model()->findByPk($id);
    if($model===null)
        throw new CHttpException(404,'The requested page does not exist.');
    return $model;
}



/**
 * Performs the AJAX validation.
 * @param CModel the model to be validated
 */
protected function performAjaxValidation($model)
{
    if(isset($_POST['ajax']) && $_POST['ajax']==='details-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
}

}

我尝试了上面的代码 - mot 发现任何结果请帮助我的朋友

谢谢德文达

4

2 回答 2

1

看到这个链接..

http://www.yiiframework.com/wiki/25/using-cautocomplete-to-display-one-value-and-submit-another/

根据此链接实现代码...自动完成将正常工作..

或者

$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'test1',
'value'=>'test21',
'source'=>$this->createUrl('Your Contoller Name / your function Name'),
// additional javascript options for the autocomplete plugin
'options'=>array(
        'showAnim'=>'fold',
),
));
于 2012-05-24T11:44:41.243 回答
0

将此添加到您的控制器...

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

              $name = $_GET['q']; 

              $criteria = new CDbCriteria;
              $criteria->condition = "first_name LIKE :sterm";
              $criteria->params = array(":sterm"=>"%$name%");

              $userArray = User::model()->findAll($criteria);
              $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'=>'first_name', 
                       //replace controller/action with real ids
             'url'=>array('admin/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-24T12:46:19.637 回答