h3rm
June 24, 2011, 3:33am
1
why dropdownlist not work in JUIDialog with Partialrender ?
I am having problems when using a DropDownList in CJUIDialog and RenderPartial.
when I select the data in the dropdown no effect at all, indicating that there is no ajax who responded.
I do this work if not in pertialrender.
To clarify I include code from pertialrender and controller.
this partial render code:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'test-form',
'enableAjaxValidation'=>true,
)); ?>
<div class="row">
<?php echo $form->labelEx($model,'country_id'); ?>
<?
echo $form->dropDownList($model,'country_id', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('site/test'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city_id', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
)));
//empty since it will be filled by the other dropdown
echo $form->dropDownList($model,'city_id', array());
?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
and this controller code :
public function actionTest(){
if (isset($_POST['country_id'])){
$tag = $_POST['country_id'];
}else{
$tag ="";
}
echo CHtml::tag('option',array('value'=>'A' ),CHtml::encode('A - ' . $tag),true);
Yii::app()->end();
}
softark
(Softark)
June 24, 2011, 5:23am
2
I think it’s because of the mismatch of dropDowList name betewwn the view and the controller.
....
echo $form->dropDownList($model,'country_id', array(1=>'USA',2=>'France',3=>'Japan'),
....
will make something like "ModelName[country_id]" for its name.
So your controller code should be …
....
if (isset($_POST['ModelName']['country_id'])){
....
h3rm
June 24, 2011, 5:49am
3
still the same.
when I debug using firebug, I do not get response when I select the contents of the DropDownList.
softark
(Softark)
June 24, 2011, 6:01am
4
And your second dropDownList also has a mismatch in IDs.
echo $form->dropDownList($model,'country_id', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('site/test'),
'update'=>'#city_id', // *** ID = "city_id"
)));
echo $form->dropDownList($model,'city_id', array()); // *** ID = "ModelName_city_id"
Should be :
echo $form->dropDownList($model,'country_id', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('site/test'),
'update'=>'#' . CHtml::activeId($model, 'city_id'),
)));
h3rm
June 24, 2011, 6:19am
5
still the same.
public function actionTest(){
not called by country_id
zaccaria
(Matteo Falsitta)
June 24, 2011, 6:37am
6
You miss the client script.
If you are really really lucky, it will work with this render partial:
$this->renderPartial('view', array(params), false, true)
The fourth parameter set to true will make Yii to include the clientscript for the ddl in the partial render and send it to the browser.
If it will not work, you can do the ddl "manually" with:
echo $form->dropDownList($model,'country_id', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'onChange' => CHtml::ajax(array(
'type'=>'POST', //request type
'data'=> "js:$(this).parent('form').serialize()",
'url'=>CController::createUrl('site/test'),
'update'=>'#' . CHtml::activeId($model, 'city_id'),
''
))));
h3rm
June 24, 2011, 6:50am
7
public function actionCreate()
{
$model = new TestForm;
if(isset($_POST['TestForm']))
{
$model->attributes=$_POST['TestForm'];
if($model->validate())
{
if (Yii::app()->request->isAjaxRequest)
{
echo CJSON::encode(array(
'status'=>'success',
'div'=>"Saved"
));
exit;
}
else
$this->redirect(array('view','id'=>$model->id));
}
}
if (Yii::app()->request->isAjaxRequest)
{
echo CJSON::encode(array(
'status'=>'failure',
'div'=>$this->renderPartial('_form_1', array('model'=>$model),false, true)));
exit;
}
else
$this->render('create',array('model'=>$model,));
}
and view
<div class="row">
<?php echo $form->labelEx($model,'country_id'); ?>
<?
echo $form->dropDownList($model,'country_id', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'onChange' => CHtml::ajax(array(
'type'=>'POST', //request type
'data'=> "js:$(this).parent('form').serialize()",
'url'=>CController::createUrl('site/test'),
'update'=>'#' . CHtml::activeId($model, 'city_id'),
''
))));
//empty since it will be filled by the other dropdown
?>
<div class="row">
<?php echo $form->labelEx($model,'city_id'); ?>
<?
echo $form->dropDownList($model,'city_id', array()); // *** ID = "ModelName_city_id"
?>
</div>
</div>
but have not succeeded, because _form_1 not appear.
tri
(tri - Tommy Riboe)
June 24, 2011, 6:58am
8
One obvious thing: if you want to use the return value from renderPartial, set param 3 to true.
/Tommy
zaccaria
(Matteo Falsitta)
June 24, 2011, 8:53am
9
Take a look at documentation of the function you are using. If in the wiki the third parameter was true you should leave it true.
h3rm
June 24, 2011, 8:59am
10
thanks for the responses.
DropDownList can function correctly.
but there is still another problem in POST data that does not exist.
at this line
'data'=> "js:$(this).parent('form').serialize()",
and in controller:
if (isset($_POST['TestForm'])){
$tag = $_POST['TestForm']['country_id'];
}else{
$tag ="";
}
echo CHtml::tag('option',array('value'=>'A' ),CHtml::encode('A - ' . $tag),true);
Yii::app()->end();
i get the value of $tag =""
h3rm
June 24, 2011, 9:05am
11
i got the solution, that i remove this line:
'data'=> "js:$(this).parent('form').serialize()",
and run well, thanks All