Yii在另一个选择上填充一个下拉列表 [英] Yii populate a dropdown on select of another

查看:132
本文介绍了Yii在另一个选择上填充一个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉,我想当选择在另一个下拉项来填充.这两个下拉列表都与从控制器传递的数据/模型相关.然后通过调用模型中的函数从数据库中填充第一个下拉列表.这是表格,

I have a dropdown that I want to populate when an item in another dropdown is selected. Both the dropdown are tied to data/model passed on from controller. and the first dropdown is populated from DB by calling a function in the model. Heres' the form,

echo $form->dropDownListRow($modelunit, 
        'superunit',
        $model->getSunits(), 
        array(
        'ajax' => array(
        'type'=>'POST',
        'url'=>CController::createUrl('user/getunits'),
        'update'=>'#unit_id',
        ))); 

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

这是Ajax调用的操作用户/getunits.

Here's the action user/getunits called by Ajax.

$data=Unit::model()->findAll('sid=:sid', 
                  array(':sid'=>(int) $_POST['superunit']));

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

选择第一个下拉菜单时,我不断收到错误消息"Undefined index:superunit".另外,您可能会注意到我在第一个下拉菜单中使用form-> dropDownListRow,而在第二个下拉菜单中使用CHtml :: dropDownList.这是因为我对如何确保使用ajax正确填充下拉列表以及正确绑定到模型的语法一无所知.

I keep getting an error "Undefined index: superunit" when first dropdown is selected. Also, you may notice I am using form->dropDownListRow for the first dropdown while using CHtml::dropDownList for the second. That's cause I am clueless on the syntax of how exactly to make sure the dropdown is populated correctly with ajax and at also properly bind to the model.

推荐答案

您使用$form->dropDownListRow,这就是在服务器端获得$_POST['MyModelName']['superunit']的原因

You use $form->dropDownListRow that's why you will get $_POST['MyModelName']['superunit'] on your server side

像这样更改代码

$data=Unit::model()->findAll('sid=:sid', 
                      array(':sid'=>(int) $_POST['MyModelName']['superunit']));

MyModelName是您使用的模型)

或喜欢

echo CHtml::dropDownList('superunit'.....

对于其他人- Wiki可能会有所帮助.

For others - this wiki may help.

这篇关于Yii在另一个选择上填充一个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆