0

我是 Yii 框架和 Ajax 的新手。所以现在我使用 aext.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  

编辑 下面是查看页面的外观 在此处输入图像描述

4

1 回答 1

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'
            )
        )); 
于 2013-11-10T05:55:39.407 回答