Yii Grid View Edit Modal

I have a link in a page which leads to a modal pop-up and after submitting the form,it populates a grid view.Now,I have an edit button in the grid view and I want to have that popup again for editing the form from my grid view.

How to do that?

1.With Ajax or Without that.

Please explain it also…

Moderators please reply to this query.

No answer.Is this forum dormant or my question is too complex????

Dear Friend

Below is a working implementation.Kindly check whether it is helpful.

I have a Model(Medico:id,name,age,sex,place).

admin.php




//Creating a link for creating records in a pop up.

<?php echo CHtml::link('Create Medico',array('medico/create'),array('id'=>'link'));?>




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'medico-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'id',

		'name',

		'age',

		'sex',

		'place',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>


<?php


$this->beginWidget('zii.widgets.jui.CJuiDialog',array(

      'id'=>'mydialog',

      'options'=>array(

          'title'=>'Update Medico',

          'autoOpen'=>false,

      ),

  ));

 

  

$this->endWidget('zii.widgets.jui.CJuiDialog');


//Below we are registering scripts.

//When user clicks link above the grid or edit button in grid,we are going to display the popup.

//When user submits the form without error, we are going to update the grid.


Yii::app()->clientScript->registerScript('edit','


$("body").on("click",".update,#link",function(e){

	$.ajax({

		type:"GET",

		url:$(this).attr("href"),

		success:function(data){$("#mydialog").dialog("open").html(data)},

		});

	return false;

	});


$("body").on("click",".butt",function(e){

	$.ajax({

		type:"POST",

		data:$("#medico-form").serialize(),

		url:($(this).attr("id")!==undefined)?"index.php?r=medico/update"+"&id="+$(this).attr("id"):"index.php?r=medico/create",

		success:function(data){$("#medico-grid").yiiGridView("update")}	

		});

	return false;

	});

');




create.php




public function actionCreate()

{

	$model=new Medico;


		// Uncomment the following line if AJAX validation is needed

	$this->performAjaxValidation($model);

	if(Yii::app()->request->isAjaxRequest)

        {	

		if(isset($_POST['Medico']))

		{

			$model->attributes=$_POST['Medico'];

			if($model->save())

			{				

				Yii::app()->end();			

			}

		}

		echo $this->renderPartial('_form',array('model'=>$model),true,true);

		Yii::app()->end();

	}


	if(isset($_POST['Medico']))

	{

		$model->attributes=$_POST['Medico'];

		if($model->save())

			$this->redirect(array('view','id'=>$model->id));

	}


	$this->render('create',array(

		'model'=>$model,

	));

}



update.php




public function actionUpdate($id)

{

	$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

	$this->performAjaxValidation($model);

		

	if(Yii::app()->request->isAjaxRequest)

        {	

		if(isset($_POST['Medico']))

		{

			$model->attributes=$_POST['Medico'];

			if($model->save())

			{				

				Yii::app()->end();			

			}

		}

		echo $this->renderPartial('_form',array('model'=>$model),true,true);

		Yii::app()->end();

	}


	if(isset($_POST['Medico']))

	{

		$model->attributes=$_POST['Medico'];

		if($model->save())

			$this->redirect(array('view','id'=>$model->id));

	}

		

        

	$this->render('update',array(

		'model'=>$model,

	));

}



_form.php




<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'medico-form',

	'enableAjaxValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,//IT IS VERY IMPORTANT TO SET "validateOnSubmit" TO true.

		),

)); ?>


</div><!-- Other form elements to follow... -->


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('class'=>'butt','id'=>$model->id)); ?>

	</div>


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


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




When user clicks the link form is displayed in a popup.

During filling the form ajaxvalidation is also enabled.

If there is no error in the form,record is created and added as last record in the grid.

You can edit the form in the same pop up after clicking the edit button.

The code above may be improved especially in limiting the number of ajax calls.

I hope you would go through it and bring out the essential changes.

Regards.

im probably a bit of topic, but i got the update working, my only concern is, when i click the update button, i get the entire page of the actionUpdate with the headers and footer, how to i assign a blank template for my pop up modal?

btw, cheers to yo seenivasan, your post has been helpful to me.