简体   繁体   English

如何在YII框架的下拉列表中显示所选值?

[英]How can I display selected value in dropdownlist in YII framework?

I want to display the selected value in the drop-down list in Yii framework. 我想在Yii框架的下拉列表中显示选定的值。 I have generated code using the Yii CRUD operation. 我已经使用Yii CRUD操作生成了代码。 While adding and updating it uses the same view ie _form.php. 在添加和更新时,它使用相同的视图,即_form.php。

    <?php echo $form->labelEx($model,'prj_id'); ?>
    <?php
    $list = CHtml::listData(ProjectList::model()->findAll(array('order' => 'prj_name')), 'prj_id',       'prj_name');
    echo $form->dropDownList($PrjList, 'prj_id', $list);
    ?>
    <?php echo $form->error($model,'prj_id'); ?>

Suppose I have country names in my dropdown. 假设我的下拉菜单中有国家名。 While adding I have selected India and saved it in the database. 添加时,我选择了印度并将其保存在数据库中。 At the time of updating it should display India as my selected country. 在更新时,它应显示印度为我选择的国家。
Thanks in advance. 提前致谢。

You're using a different model for the dropdown..? 您为下拉菜单使用了其他模型。

If you use $model as the model for the dropdownlist you'll save the ID of the selected value to the database. 如果将$model用作下拉列表的模型,则将所选值的ID保存到数据库中。 So then when you're going to update the record, the $model->prj_id will be set to the saved value, so that is the value it will display. 因此,当您要更新记录时, $model->prj_id将被设置为保存的值,因此它将显示该值。

Don't know what $PrjList is, but I think it should be like the following code, since you're also displaying the label and error for this model and field. 不知道$PrjList是什么,但是我认为它应该类似于以下代码,因为您还将显示此模型和字段的标签和错误。

  echo $form->dropDownList($model, 'prj_id', $list);

If for some reason you do need $PrjList as the model, make sure the prj_id is set to the saved value. 如果由于某种原因您确实需要$PrjList作为模型,请确保将prj_id设置为保存的值。

$form->dropDownList is same as CHtml::activeDropDownList . $form->dropDownListCHtml::activeDropDownList相同。 You have to pass model object and then that object has dropdown attribute set it will select it automatically. 您必须传递模型对象,然后该对象具有下拉属性设置,它将自动选择它。

使用以下格式

<?php  echo $form->dropDownList($PrjList,'prj_id', CHtml::listData(ProjectList::model()->findAll(array('order' => 'prj_name')),'prj_id','prj_name'),array('prompt'=>'Select Parent Menu','class'=>"span6 m-wrap"));?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM