开发者

Yii jui.CJuiAutoComplete with JS function as source

i'm using zii.widgets.jui.CJuiAutoComplete widget from the Yii framework, where autocompletion works fine, as log as there is an URL as source for the suggestions.

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

But now I need the suggestions coming from an javascript function, like this:

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

But it seems not to be working.

Is it possible to use an function as source? How should the javascript function be designed? I tried something like this:

function autoCompleteTags{

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

(This is just an simple example. The JS function code will be replaced by some more complex code. But the situation will be the same)

Thank you for any kind of help!


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


widget code is

<?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'
                            ),
                        ));
                        ?> 

pass values from database to widget

<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜