2011-05-04 11 views
1

我使用了Yii框架中的zii.widgets.jui.CJuiAutoComplete小部件,其中自動完成工作正常,因爲日誌中有一個URL作爲建議的源。Yii jui.CJuiAutoComplete用JS函數作爲源碼

 $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
         'model'=>$model, 
         'source'=>$this->createUrl('ajax/autoComplete'), 
[...] 

但現在我需要建議從javascript函數來了,就像這樣:

'source'=>'js: autoCompleteTags();' 

但似乎並不奏效。

是否可以使用函數作爲源? javascript函數應該如何設計? 我想是這樣的:

function autoCompleteTags{ 

    var Tags= [ 
     "ActionScript", 
     "AppleScript", 
     "Asp", 
     "BASIC", 
     "C", 
     "C++", 
    ]; 
    return Tags; 
    } 

(這只是一個簡單的例子JS功能代碼將通過一些更復雜的代碼來替換但是現在的情況是一樣的。)

謝謝你任何形式的幫助!

回答

0
var Tags = [ 
     { 
      value: "jquery", 
      label: "jQuery", 
     }, 
     { 
      value: "jquery-ui", 
      label: "jQuery UI", 
     }, 
     { 
      value: "sizzlejs", 
      label: "Sizzle JS", 
     } 
    ]; 
0

插件代碼是從數據庫

<?php 
         $ingredients = Ingredient::getingredients(); 
         $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
          'name' => 'ingredient[]', 
          //'source' => $ingredients, 
          'options' => array(
           'change' => 'js:function() { 
           var context=$(this); 
           var childingredient = $(this).val(); 
           alert(childingredient); return false; 
           $.ajax({ 
           type:"post", 
           url : "' . Yii::app()->createAbsoluteUrl('recipe/getgramweight') . '", 
           data: "parentingredient=" + parentingredient, 
           success : function (response){ 
           context.parent().next().find("select").html(response); 
           }, 
           }); 
           }', 
           'minLength' => '1', 
          ), 
          'htmlOptions' => array(
           'style' => 'height:35px;', 
           'class' => 'ingredientname' 
          ), 
         )); 
         ?> 

值傳遞給小部件

<?php 
    $criteria = new CDbCriteria(); 
    $criteria->select = "ingredient"; 
    $criteria->condition = 'ingredient!=""'; 
    $ingredients = Mymodel::model()->findAll($criteria); 
    $ings = "["; 
    foreach ($ingredients as $value) 
    { 
     $ings.='"' . $value->ingredient . '"' . ','; 
    } 
    $ings.="]"; 
    $ingredients_array = explode(",]", $ings); 
    $ingredients_array[0] . "];"; 
    ?> 



      }); 
     }); 
    </script>