Gridview - Redirect To The Same Page

How can I go back to the same page in GridView after updating a particular record.

For example, when I click an update icon in the button column of a row on GridView page 5 then I get redirected to page 1 after saving the record changes.

Thank you for hints.

Dear Friend

Here is one example.

Model:Wage,

Controller:WageController.

WageController.php




public function actionAdmin()

{

        $model=new Wage('search');

        $model->unsetAttributes();  // clear any default values         

        if(isset($_GET['Wage']))

        {

                $model->attributes=$_GET['Wage'];

        }

        Yii::app()->user->setState('grid',$_GET);//saving all the get parameters into session.


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

                        'model'=>$model,

                ));

}



views/wage/view.php.

Just append the following in view.php




if(Yii::app()->user->hasState("grid"))

{       

        $grid=Yii::app()->user->getState('grid');               

        $route=array("wage/admin");

        echo CHtml::link("Return to Grid",array_merge($route,$grid));

        Yii::app()->user->setState("grid",null);

}



Now you will exactly land from where you came.

It retains previous sorting and page as well.

The only drawback here is ugly looking url.

I hope I helped a bit.

Regards.

Thank you very much, this works perfectly. I created the link at bottom of the view and it brings me back to where I came from. Now I am happy to understand a little, how yii manages sessions.

Please can you help me, how to change also the menu link back to the admin page on this view. I am a yii beginner and I have much troubles to understand the logic and the object structure of yii …

Please can you help me to change the menu item like that:


array('label'=>'Noten bearbeiten', 'url'=>array('admin')),

Best regards

Ferdinand

Dear Friend

In my case I achieved this in the followin way.




array('label'=>'Manage Wage', 'url'=>$this->createUrl('admin',Yii::app()->user->hasState("grid")?Yii::app()->user->getState('grid'):"")),



Kindly apply this in your scenario.

Regards.

Hi,

thank you very much.

After applying this as menu item


array('label'=>'Noten bearbeiten','url'=>$this->createUrl('admin',Yii::app()->user->hasState("grid")?Yii::app()->user->getState('grid'):"")),

I get the error


Fatal error: Cannot unset string offsets in /srv/www/yii-1.1.12.b600af/framework/web/CUrlManager.php on line 283

To find the bug easier, please look at more code of the view:


...

$params = "";

if(Yii::app()->user->hasState("grid")) {

        $params = Yii::app()->user->getState('grid');

        Yii::app()->user->setState("grid",null);

}


$this->menu=array(

	array('label'=>'Alle anzeigen', 'url'=>array('index')),

        ...

// 	array('label'=>'Noten bearbeiten', 'url'=>array('admin')),

	array('label'=>'Noten bearbeiten',

              'url'=>$this->createUrl('admin',Yii::app()->user->hasState("grid")?$params:"")),

);


...


echo CHtml::link("Zurueck",array_merge('noten/admin',$params));

Thanks in advance

Ferdinand

Dear Friend

Sorry about that.

It committed a mistake.

The following is the code from my machine.




$this->breadcrumbs=array(

	'Wages'=>array('index'),

	$model->name,

);


$this->menu=array(

	array('label'=>'List Wage', 'url'=>array('index')),

	array('label'=>'Create Wage', 'url'=>array('create')),

	array('label'=>'Update Wage', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Wage', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Wage', 'url'=>$this->createUrl('admin',Yii::app()->user->hasState("grid")?Yii::app()->user->getState('grid'):array())),

);

?>


<h1>View Wage #<?php echo $model->id; ?></h1>


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

	'data'=>$model,

	'attributes'=>array(

		'id',

		'name',

		'days',

		'hours',

	),

)); ?>


<?php


if(Yii::app()->user->hasState("grid"))

		{	

			$grid=Yii::app()->user->getState('grid');		

			$route=array("wage/admin");

			echo CHtml::link("Return to Grid",array_merge($route,$grid));

			Yii::app()->user->setState("grid",null);

		}




I appended a empty string rather than attaching an empty array in ternary conditional operator.This caused the problem.

it should be




Yii::app()->user->hasState("grid")?Yii::app()->user->getState('grid'):array() //earlier I attached an empty string.



As we are making "grid" status null, the code to create the menu should come above it before making it null.

Regards.

Thank you very much. Now it works in both ways: using a link and using a menu item!

Regards

Ferdinand