Php 插入新元素后更新Yii Dropdownlist
这是我的密码:Php 插入新元素后更新Yii Dropdownlist,php,javascript,jquery,ajax,yii,Php,Javascript,Jquery,Ajax,Yii,这是我的密码: <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' )); ?>
<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>
控制器的代码是一个简单的创建函数:
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(); ?>
在我的PhoneTypesController中:
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,
));
}
公共函数actionCreate(){
$model=新的电话类型;
如果(isset($_POST['PhoneTypes'])){
$model->attributes=$\u POST['PhoneTypes'];
如果($model->save()){
echo CHtml::脚本(“
window.parent.$('dialog')。dialog('close');
window.parent.$('#Phone_types_id').append(''+'.$model->type.'+'.);
");
Yii::app()->end();
}
}
$this->render('create',数组(
'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函数的成功回调中使用此输出向select(即下拉列表)添加新选项:
jQuery.get(//您的“数据”和“url”在这里
成功:功能(数据){
$(“#公司_电话_类型”).append(“”+data.label+“”);
});
有关如何在jQuery中执行此操作的更多信息,请参阅。Hi,@SamuelLiew。谢谢你抽出时间。我把控制器和对话框的代码放在上面。谢谢你的回复!看来这正是我要找的!我会准备好并适应我的场景!祝你一切顺利!
<?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"}
}
}
?>
jQuery.get( // your 'data' and 'url' here
success: function(data) {
$('#Company_phone_type').append('<option value="' + data.value + '">' + data.label + '</option>');
});