dongzhui2636 2014-11-13 12:03
浏览 43
已采纳

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

I want to display the selected value in the drop-down list in Yii framework. I have generated code using the Yii CRUD operation. While adding and updating it uses the same view i.e. _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.

  • 写回答

3条回答 默认 最新

  • douguomou5094 2014-11-13 12:21
    关注

    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. 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.

    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.

      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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据