我是 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
编辑 下面是查看页面的外观