2012-05-25 41 views
0

我試圖在我的項目中實現CJuiAutoComplete,但它不工作。過去幾天我研究了這個問題,並嘗試了一切。看起來發生了什麼(或者在這種情況下沒有發生)是控制器中的查找操作沒有被調用。如果將源設置爲簡單的項目數組,我也無法使其工作。我究竟做錯了什麼?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"; 
    } 
+0

您是否嘗試過'sourceUrl' insted的'source'?即''sourceUrl'=> array('address/lookup')' – dInGd0nG

+0

是的,我也嘗試了sourceUrl。我很難過! – nimble

+0

@ user1417526可能是您在同一個視圖文件中使用同一模型的'zipcode'屬性定義了一個小部件(一個textField或某物)。在這種情況下,自動完成可能無法工作 – dInGd0nG

回答

0

啊,jui's autocomplete預計以下列格式數據:

預期數據格式

從本地數據的數據,一個URL或一個回調可以有兩種變體:

字符串Array: [「選擇1」,「選擇2」]
與標籤和值的屬性的對象Array: [{標籤: 「選擇1」,值: 「VALUE1」},...]

所以在你的行動,你一定要返回JSON:

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

編輯:CJSON docs

+0

我現在看到我的CJuiDatePicker小部件不再工作,螢火蟲控制檯說jQuery沒有定義。我只能假設這兩個問題都是相關的。也許它是一個配置問題? – nimble

+0

你檢查過jquery是否包含在生成的html中嗎? –

+0

謝謝你的幫助是非常感謝。錯誤發生在多個頁面上。然而,關於這個生成的代碼加載jquery.js然後jquery.yiiactiveform.js。 – nimble

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 

      )); 
    ?> 

其做工精細...

+0

由於CAutoComplete現在已被棄用,我寧願留下一些長期支持的東西。這就是我選擇jui widget的原因。 – nimble

0

它jQuery的問題,請diable或刪除jquery-min.js文件,然後檢查其工作正常。