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 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?