yii中的动态下拉列表

时间:2013-02-05 05:17:54

标签: php yii

我按照维基中给出的说明在yii中创建动态下拉列表,但仍然没有生成第二个下拉列表, 这是我的视图文件:

echo CHtml::dropDownList('AccountTypeID','', array(1=>'Admin',2=>'Manager',3=>'Business',4=>'Finance',5=>'Customer Support'),array('ajax' => array('type'=>'POST',
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
'update'=>'#city_id', //selector to update
))); 


echo CHtml::dropDownList('city_id','', array());

这是我的控制器:

public function actionDynamiccities(){
$data= RefAccountgroup::model()->findAll('parent_id=:parent_id', 
              array(':parent_id'=>(int) $_POST['AccountTypeID']));

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

4 个答案:

答案 0 :(得分:2)

请注意 'ajax' 声明

'ajax' => array(
 'type'=>'POST',
 'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
 'update'=>'#city_id', //remember to change it according to this rules #modelname_columnname
 'data'=>array('AccountTypeID'=>'js:this.value'),   //I think you missed this line
 )));

如果您的firefox浏览器上安装了firebug,请尝试查看控制台选项卡,并检查您的下拉列表是否成功更改发送参数 AccountTypeID ,您也可以看到响应。< / p>

答案 1 :(得分:0)

首先,如果您确实包含了您尝试遵循的教程的链接,那么您的问题就会得到改善。

其次,在评论中回答问题时,“它没有发送数据”并没有真正澄清任何内容。此时,我不确定您是否查看了Firebug或任何Web开发人员工具来检查发布的内容和收到的内容。我的猜测是,如果你看起来你会看到一个POST请求,没有数据被发送,也没有返回数据。

您的AJAX调用没有数据参数,因此不会定义$_POST['AccountTypeID'],这意味着控制器中的$data也将为空。

更新您的AJAX以POST AccountTypeID,例如:

echo CHtml::dropDownList('AccountTypeID',
                         '', 
                         array(1=>'Admin',2=>'Manager',3=>'Business',4=>'Finance',5=>'Customer Support'),
                         array('ajax' => array('type'=>'POST',
                                               'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
                                               'update'=>'#city_id', //selector to update
                                               'data'=> '$("#AccountTypeID").serialize()',
))); 


echo CHtml::dropDownList('city_id','', array());

您可能需要调整数据参数以正确解析正确的输入以获取AccountTypeID - 我只是在上面的示例代码中猜测。

然后,在测试时,请务必使用Firebug或类似的方法来检查POST的内容,并确保传递了AccountTypeID。

答案 2 :(得分:0)

很简单。您需要在模型上声明一个与下拉列表同名的公共变量。防爆。  在你的模型中 公开mydropdown;

在您的视图中

echo $ form-&gt; dropDownList($ model,'mydropdown',array('1'=&gt;'hello','2'=&gt;'bye'));

答案 3 :(得分:-1)

不确定这是否有帮助,但......

echo CHtml :: dropDownList('city_id','',array('id'=&gt;'city_id'));

相关问题