将CHtml :: dropDownList更改为$ form-> dropDownList

时间:2013-12-18 19:03:33

标签: drop-down-menu yii

视图

 <div class="form">

 <?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'sendmessage-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
    'validateOnSubmit'=>true,
),
 )); ?>
<div class="row">
    <?php echo $form->labelEx($model,'receiverType'); ?>
    <?php echo $form->dropDownList($model,'receiverType',array(1 => 'type1' ,2=>'type2'),
    array(
        'empty'=>"",
        'value'=>'1',
        'ajax' => array(
            'type'=>'POST', 
            'url'=>CController::createUrl('site/type'),             
            'data'=>array('type'=>'js:this.value'),
            'dataType'=>'html',
            'update' => '#receiver',
        ))); ?>
    <?php echo $form->error($model,'receiverType'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'receiver'); ?>
    <?php echo CHtml::dropDownList('receiver',$model->receiver,array()); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'subject'); ?>
    <?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>128)); ?>
    <?php echo $form->error($model,'subject'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'body'); ?>
    <?php echo $form->textArea($model,'body',array('rows'=>10, 'cols'=>100,'style'=>'width: 60%')); ?>
    <?php echo $form->error($model,'body'); ?>
</div>

<div class="row buttons">
    <?php echo CHtml::submitButton('ارسال'); ?>
</div>

   <?php $this->endWidget(); ?>

  </div><!-- form -->

控制器

public function actionType()
{
    $Type=$_POST["type"];
    $contact=Yii::app()->db->createCommand()
                ->select('id,email')
                ->from('contact')
                ->order('parent.email')
                ->queryAll();
    $data=CHtml::listData($contact,'id','email');
    if (!empty($data)) { echo '<option value="">choose...</option>'; }
    foreach($data as $value=>$name) {
        echo CHtml::tag('option',
                array('value'=>$value),CHtml::encode($name),true);
        }
}

它正常工作,但当我更改此行时: 了CHtml :: DROPDOWNLIST( '接收器',$模型 - &GT;接收机,阵列()); 对此: $形式 - &GT; DROPDOWNLIST($模型, '接收器',阵列()); 它不再起作用了。 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

它无效的原因是因为下拉列表的id不再是#receiver。您有两个选择:

  1. 将要更新的ID更改为下拉列表的新ID,即#{Model_Name}_receiver
  2. 明确将下拉列表的ID设置为#receiverhtmlOptions
  3. 我会选择第一个与表单的其他输入保持一致。

答案 1 :(得分:-2)

你的模特中有接收者吗?

你必须

// in a view  to use this
$form->dropDownList($model,'receiver',array()); 

// you should be having this in your
public $reciever; 
// it should be in your model to use it in view 

当你打电话给ajax时

  'ajax' => array(
        'type'=>'POST', 
        'url'=>CController::createUrl('site/type'),             
        'data'=>array('type'=>'js:this.value'),
        'dataType'=>'html',
       // 'update' => 'data.receiver',
        'update' => '#{id of reciver element perhaps it will be  
"#sendmessage-form_reciever"

更新#sendmessage-form_reciever ,默认为该元素的ID