更新字段不会在Yii中加载值

时间:2014-05-20 11:17:42

标签: php yii yii-extensions

在我看来,我有一个下拉字段和一个文本框。这会动态变化。这工作正常。它接受值并存储,但是当我执行Update时,从Db中检索该值,但不会在相应的框中填充。

我的观点:

<table width="100%" border="0" align="center">
<tr>            
    <td>
        <?php echo $form->labelEx($model,'cm_classification_id'); ?>        
        <?php echo $form->dropDownList($model,'cm_classification_id', CHtml::listData(masterClassification::model()->findAll(array('order' => 'cm_classification_id ASC', 'condition'=>'cm_classification_type=:type', 'params'=>array('type'=>'initiate'))), 'cm_classification_id', 'cm_classification_name'),  array('empty'=>'Select classification')); ?>
        <?php echo $form->error($model,'cm_classification_id'); ?>
    </td>
<tr>
    <td colspan="3">
    <label>Change Description <span class="required" id="desc_req_note" style="display:none;">*</span></label>
    <span id="cm_desc_select" style="display:none;"><?php echo $form->dropDownList($model,'cm_description',array(),array('empty'=>'Select Change Description')); ?></span>
     <span id="cm_desc_input" style="display:none;"><?php echo $form->textField($model,'cm_description1'); ?></span>
    <?php //echo $form->dropDownList($model,'cm_description',array(),array('empty'=>'Select Change Description')); ?>
    <?php echo $form->error($model,'cm_description'); ?>

    </td>
</tr>
</table>

<script>
    $(document).ready(function() {
    $(cm_desc_select).show();
});
    $('#CMMaster_cm_classification_id').change(function(){

     var val = $(this).val();
     var cm_desc_select_elem = $('#cm_desc_select'); 
     var cm_desc_input_elem = $('#cm_desc_input');
     if(val != 3) 
     { 
         $(cm_desc_input_elem).show();
         $(cm_desc_select_elem).hide();
     } 
     else 
       { 
         $(cm_desc_select_elem).show();
         $(cm_desc_input_elem).hide();
        }
});</script>

我的行动:

public function actionUpdate($id) {
        $model = $this->loadModel($id);
}

分类字段自动从DB加载,但描述字段的空值通过数据从DB中获取。

功能:

public function loadModel($id) {
        $model = CMMaster::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

2 个答案:

答案 0 :(得分:1)

$('#DROPDOWN_ID').val('<?php echo $model->cm_description; ?>');

在View中使用JQuery完成。

答案 1 :(得分:0)

您的代码不清楚,但我们假设您正在从cmm_master表中读取下拉数据。

在您的表单中,更改

<?php echo $form->dropDownList(
         $model,
         'cm_description',
         array(),        // <=== You are not passing data //
         array('empty'=>'Select Change Description'));
?>

<?php echo $form->dropDownList($model,
         'cm_description',
          CHtml::listData(CMMaster::model()-‌​>findAll(),'cmm_id', 'cm_description'),
          array('empty'=>'Select Change Description'));

&GT;