Hello !
I am struggling for 3 days now with this simple task to update a div using ajax and renderpartial. Please take a look and let me know what do you found wrong. At the moment the controller action is working fine, but is not updating the div (partialb_example[’.$j.’]). $j is a counting loop because there more than one form and partial views.
Controller b_example:
public function actionConfirm($id_b_example)
{
$j = $_GET['j'];
if(isset($_POST['b_example']))
{
$model=$this->loadModel($id_b_example);
$model->attributes=$_POST['b_example'];
$model->id_status = 10; //set status of confirm for other b_example
if($model->validate())
{
if($model->save())
{
$model_parent=$this->loadModel($model->parent);
$model_parent->id_status = 10;
if($model_parent->save())
{
$other_b_examples = $this->get_b_example_products_status(array(2,3));//hardcoded only for testing
$b_example_model = new b_example;
$this->renderPartial('/b_example/_b_examples', array(
'other_b_examples'=>$other_b_examples,
'j'=>$_GET['j'],
'b_example_model'=>$b_example_model,
), false, true);
}else
throw new CHttpException(500,'Can not save parent actionConfirm.');
}
else
throw new CHttpException(500,'Can not save actionConfirm.');
}
else
{
throw new CHttpException(500,'Can not validate actionConfirm.');
}
}
}
b_examples View:
echo '<div id="partialb_example['.$j.']">';
$this->renderPartial('/b_example/_b_examples',array(
'other_b_examples'=>$other_b_examples,
'j'=>$j,
'b_example_model'=>$b_example_model,
));
echo '</div>';
Partial view
$form=$this->beginWidget('CActiveForm', array(
'id'=>'b_example['.$j.']',
'enableAjaxValidation'=>false,
'action'=>Yii::app()->createUrl('/b_example/acceptb_example'),
));
echo $form->hiddenField($b_example_model,'id_user',array('value'=>Yii::app()->user->id));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$other_b_examples,
'id'=>'b_example-offer-grid['.$j.']',
'template'=>'{items}{pager}',
'columns'=>array(
'rel_product.title',
array(
'class'=>'CButtonColumn',
'header'=>Yii::t('strings','Actions'),
'buttons'=>array(
'Confirm' => array(
'label'=>Yii::t('strings','Confirm'),
'url'=>'Yii::app()->createUrl("/b_example/confirm",array("id_b_example"=>$data->id_b_example,"j"=>'.$j.'))',
'options'=>array(
'ajax'=>array(
'type'=>'POST',
'url'=>"js:$(this).attr('href')",
'update'=>'#partialb_example['.$j.']',
),
),
),
'template'=>'{Confirm}',
),
)));
$this->endWidget();
This is some of the html jquery code:
jQuery('#b_example-offer-grid[0]').yiiGridView({'ajaxUpdate':['b_example-offer-grid[0]'],'ajaxVar':'ajax','pagerClass':'pager','loadingClass':'grid-view-loading','filterClass':'filters','tableClass':'items','selectableRows':1,'enableHistory':false,'updateSelector':'{page}, {sort}','filterSelector':'{filter}','pageVar':'b_example_page'});
jQuery('body').on('click','#yt0',function(){jQuery.ajax({'type':'POST','url':$(this).attr('href'),'cache':false,'data':jQuery(this).parents("form").serialize(),'success':function(html){jQuery("#partialb_example[0]").html(html)}});return false;});