Yii- Ajax值不会被触发到控制器中

时间:2013-11-10 05:51:49

标签: php ajax yii

我是Yii框架和Ajax的新手。所以我现在使用 ext.combobox.EJuiComboBox 来创建下拉列表。下面是我用来在视图文件中创建下拉列表的代码:

<?php
$this->widget('ext.combobox.EJuiComboBox', array(
    'model' => $model,
    'attribute' => 'company_id',
    // data to populate the select. Must be an array.
    //'data' => $model->getAllModels(),
    'data' => CHtml::listData(Company::model()->findAll(), 'id', 'name'),
    // options passed to plugin
    // Options passed to the text input
    'options' => array(
        // JS code to execute on 'select' event, the selected item is
        // available through the 'item' variable.
        'onSelect' => CHtml::ajax(array(
                'type'=>'POST',
                'url'=>CController::createAbsoluteUrl('bill/getProjects'),
                'update'=>'#'.CHtml::activeId($model,'project_id'),
                                'beforeSend' => 'function(){
                                    $("#page").addClass("loading");}',
                                'complete' => 'function(){
                                    $("#page").removeClass("loading");
                                    $("#' . CHtml::activeId($model,'project_id') . '").trigger("change");
                                }',
                               'success'=>"function(){
                alert('ok');}"

            )),
        // JS code to be executed on 'change' event, the input is available
        // through the '$(this)' variable.

        'allowText' => false,
    ),
    // Options passed to the text input
    'htmlOptions' => array('style'=>'width:70px'
            )
)); ?>

现在,ajax被激活到控制器,但是project_id的值仍为0.现在在控制器中我有以下功能。警报框被执行。

public function actionGetProjects()
    {
        $data=Project::model()->findAll('company_id=:company_id',
            array(':company_id'=>(int) $_POST['Bill']['company_id']));

        $data=CHtml::listData($data,'id','name');
        foreach($data as $value=>$name)
        {
            echo CHtml::tag('option',
                array('value'=>$value),CHtml::encode($name),true);
        }
    }

在生成的日志中我可以看到虽然我从值中选择了company_id,但comapny_id表示值为0.我该如何调试它?

SELECT * FROM `project` `t` WHERE company_id=0  

修改 下面是视图页面的外观 enter image description here

1 个答案:

答案 0 :(得分:0)

您可以通过设置html选项的名称来解决此问题

'htmlOptions' => array('style'=>'width:70px' , 'name' => 'Bill[company_id]' ),

更新:我正在将此设置用于小部件

     $this->widget('ext.combobox.EJuiComboBox', array(
            'id' => 'agentAuto',
            'model' => $model,
            'attribute' => 'id',
            'htmlOptions'=>array('name'=>'Calendar[name]'),
            'options' => array(
                'item' => array(
                    'label' => 'name',
                    'value' => 'id'
                ),
                'path' => 'CallBackData.User',
                'afterChange'=>'fillDropdowns(el.element)',
                'url' => Yii::app()->baseUrl.'/user/AutoList?limit=10'
            )
        ));