插入新元素后更新Yii Dropdownlist


Updating Yii Dropdownlist After Insert New Element

这是我的代码:

<p>
  <?php echo $form->labelEx($model,'phone_type'); ?>
  <span class="field">
  <?php echo $form->dropDownList($model,'phone_type', 
     CHtml::listData(PhonesTypes::model()->findAll(),
 'id','type' )); ?>     
  <?php echo $form->error($model,'phone_type'); ?>
</span>                                 
</p>

将有一个按钮来注册新的电话类型。因此,在CJUiDialog内部的表单提交之后,我希望上面的dropDownList更新为新类型,而不刷新页面。

我搜索了很多,但我只在Yii中找到了与"依赖性下降"相关的东西。

解决这个问题的更好方法是什么?有类似$.fn.cgridview.update的东西吗?

这是对话框代码:

<?php  $this->endWidget('zii.widgets.jui.CJuiDialog');
  $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
   'id'=>'dialog-crud',
   'options'=>array(
    'title'=>'Create new Phone Type',
    'autoOpen'=>false,
    'modal'=>true,
    'width'=>1080,
    'height'=>820,
    'resizable'=>false
    ),
  ));
 ?>
<iframe src="http://myapp/phone_types/create"  width="100%" height="100%"></iframe>
<?php $this->endWidget(); ?>

而控制器的代码,是一个琐碎的创建函数:

public function actionCreate(){
$model = new PhoneType;
if(isset($_POST['PhoneType'])){  
  $model->attributes = $_POST['PhoneType'];
  if( $model->save() ){
    //----> some suggestion here? echo CHtml::script("");
    Yii::app()->end();
  }
 }
}

因此,下面是我的解决方案的代码视图中:

<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
  'id'=>'dialog',
  'options'=>array(
    'title'=>'Phone Types',
    'autoOpen'=>false,
    'modal'=>true,
    'width'=>1080,
    'height'=>820,
    'resizable'=>false
  ),
  ));
 ?>
 <iframe src="phoneTypes/create" id="cru-frame" width="100%" height="100%"></iframe>
 <?php $this->endWidget(); ?>

在我的手机类型控制器:

public function actionCreate(){
    $model = new PhoneTypes;
    if(isset($_POST['PhoneTypes'])){
        $model->attributes = $_POST['PhoneTypes'];
        if($model->save()){             
            echo CHtml::script("
                window.parent.$('#dialog').dialog('close');
                window.parent.$('#Phone_types_id').append('<option value=".$model->id." >'+'".$model->type."'+'</option>');
            ");
            Yii::app()->end();                
        }   
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

您可能有一个添加电话类型的操作(在本例中,我们称之为phoneType/create)。

当您向该操作发送ajax请求以创建您的电话类型时,该操作应该返回新创建的电话类型信息。然后可以使用jQuery将其添加到下拉列表中。

看看这个例子:

<?php
// In protected/controllers/PhoneTypeController.php
public function actionCreate($phoneType)
{
    $phoneType = new PhoneType;
    $phoneType->phone_type = $phoneType;
    if ($phoneType->save())
    {
        echo CJSON::encode(array('value' => $phoneType->id, 'label' => $phoneType->phone_type)); // echos something like {"value":5,"label":"test"}
    }
}
?>

对于本例的其余部分,我将假设您的原始模型(您有一个phone_type字段的模型)称为Company(这对下面的jQuery代码有影响,我在这里选择您的phone_type下拉列表)。

然后,您可以在ajax函数的成功回调中使用此输出,为您的选择(即下拉菜单)添加一个新选项:

jQuery.get( // your 'data' and 'url' here
    success: function(data) {
        $('#Company_phone_type').append('<option value="' + data.value + '">' + data.label + '</option>');
});

有关如何在jQuery中执行此操作的更多信息,请参阅使用jQuery/javascript向选择添加选项。