Yii Framework Forum: Yii Grid View Edit Modal - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii Grid View Edit Modal Edit Button Rate Topic: -----

#1 User is offline   YearningForYii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 21-September 12

Posted 26 March 2013 - 02:13 AM

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..
0

#2 User is offline   YearningForYii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 21-September 12

Posted 26 March 2013 - 05:22 AM

Moderators please reply to this query.

View PostYearningForYii, on 26 March 2013 - 02:13 AM, said:

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..

0

#3 User is offline   YearningForYii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 21-September 12

Posted 29 March 2013 - 05:29 AM

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

View PostYearningForYii, on 26 March 2013 - 05:22 AM, said:

Moderators please reply to this query.

0

#4 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 29 March 2013 - 09:27 AM

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.
0

#5 User is offline   Neovash 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 09-November 13
  • Location:Philippines

Posted 02 February 2014 - 10:58 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users