How to return to current page in GridView?

Thanks, Frocco! :)

I knew about Antonio and the gridview, but I wasn’t able to find it.

Didn’t occur to me to check out his blog.

You saved me some time.;)

This confused me for a while, before I actually understood what the problem was! :P

So basically, if I have understood correctly, you are adding custom buttons which on clicking should perform some kind of background action (through a specific route) before updating the grid to reflect whatever change has been made through this background action.

In which case, Jacmoe is right, and the code used in CButtonColumn for deletion is what you should base yourself on. Instead of having the ajax just send a request to your action and using the result for updating the grid (I’m not quite clear exactly what you’re doing here), you should copy the delete way:


$.fn.yiiGridView.update('{$this->grid->id}', {

		type:'POST',

		url:$(this).attr('href'),$csrf

		success:function(data) {

			$.fn.yiiGridView.update('{$this->grid->id}');

		},

		error:function() {

		}

	});

	return false;

This uses the provided GridView update function to first execute your action through an ajax call (the action should not need to do any redirection or return anything, since no update will be made directly from it), then on success to make another ajax call to update the grid (and since the success ajax call does not specify a url, the GridView update will use the one that is specified in the hidden keys div, which will contain all the parameters that are in use for the current grid (page, sort etc).

This is equally valid whether you are using the standard CGridView or RRGridView (although you’ll only have browser back support with RRGridView).

On a side note, any parameters that come after the hash are for the client browser only - they are never sent to the server, so it is perfectly normal that Yii::app()->request->getUrlReferrer() shouldn’t take it into account since it cannot.

I hope I’ve been reasonably clear, and also that I have understood what you’re trying to do.

Having said all that, I realise I am only taking into consideration cases where javascript is activated. So for those where it is not, your action should indeed perform a redirect to the referring url (which, since javascript is disabled, should contain all the page and sort parameters in the querystring, and so should go back to the right page), although to save on processing and client requests, you can test whether the request is an ajax one or not, and only make the redirect if it is not.

One solution would be to store the entire url in the session in a onClick event (or whatever) before executing the action - I wonder if RRViews could help in doing that?

The problem is - for instance - when you use the update button… How do you redirect to the grid after you’ve done?

The only other option would simply be to use a JUiDialog and never leave the grid. :)

That’s probably a good solution for update.

(View doesn’t need a redirect, obviously. Your users have the back-button and you have RRViews to make the back button work.)

If you only grab the current page parameter, you’d lose filters.

Yes, the user clicks on a custom button on the grid that just updates the record as being checked out and no one else can edit it. I also have a check in function. I am not that good at ajax, but would like to try and get your solution working.

Glad to help, I appreciate your time given,

@Jacmoe - CGridView has a beforeAjaxUpdate parameter which would allow you to execute a javascript function (which might, for instance, send the current url with hash to a special action) before actually updating the grid (as long as the update uses the $.fn.yiiGridView.update function), but given Frocco’s requirements, as far as I can see using the same kind of code as the delete button should do the trick.

@Frocco - I think this should do the trick:


// set the id to be used for the grid

$gridId = 'editGrid';

// add CSRF code if CSRF validation is activated

$csrf = '';

if(Yii::app()->request->enableCsrfValidation) {

    $csrf = "\n\t\tdata:{ '".Yii::app()->request->csrfTokenName."':'".Yii::app()->request->csrfToken."' },";

}

// generate the update function (NB POST should be used for anything which alters state)

$ajaxGridButtonFunction = '$.fn.yiiGridView.update(\''.$editGrid.'\', {

    type: \'POST\',

    url: $(this).attr(\'href\'), '.$csrf.'

    success: function(data) {

        $.fn.yiiGridView.update(\''.$editGrid.'\');

    },

    error:function() {

    }

});

return false;

You would place this code before you initialise the GridView widget, then you would assign the same id to the GridView:


'id' => $editGrid,

and assign the javascript function we generated to your button click event:


'buttons' => array(

    'checkout' => array(

        'click' => $ajaxGridButtonFunction,

        'label' => 'Check Out',

        'visible'=>'$data->CHKFLAG<>"Y"',

        'url'=>'array("prjmaster/CheckOut", "id"=>$data->ID)',

        'imageUrl' => 'images/lock.jpg',

    ),

    'checkin' => array(

        'click' => $ajaxGridButtonFunction,

        'label' => 'Check In',

        'visible'=>'$data->CHKFLAG=="Y" && $data->CHKOUTUSR == Yii::app()->user->name',

        'url'=>'array("prjmaster/CheckIn","id"=>$data->ID)',

        'imageUrl' => 'images/unlock.jpg',

    ),

),

You should be able to use the same function for any similar buttons that update a state without redirecting the view.

Bear in mind as I said in my second reply, that you do want to keep the redirects in your action, for the case where javascript is not enabled in the client browser.

Rupert,

Thank you so much for taking the time to code this.

I will study it as I want to learn more about using ajax requests.

Regards,

Frank

No problem. Do let me know if it works like that (I haven’t actually implemented it myself).

Having some trouble, nothing happens when I click.




<?php

// set the id to be used for the grid

$editGrid = 'editGrid';

// add CSRF code if CSRF validation is activated

$csrf = '';

//if(Yii::app()->request->enableCsrfValidation) {

    $csrf = "\n\t\tdata:{ '".Yii::app()->request->csrfTokenName."':'".Yii::app()->request->csrfToken."' },";

//}




// generate the update function (NB POST should be used for anything which alters state)

$ajaxGridButtonFunction = '$.fn.yiiGridView.update(\''.$editGrid.'\', {

    type: \'POST\',

    url: $(this).attr(\'href\'), '.$csrf.'

    success: function(data) {

        $.fn.yiiGridView.update(\''.$editGrid.'\');

    },

    error:function() {

    }

});

return false;';

?>


array(

			'class'=>'CButtonColumn',

			'deleteButtonOptions' => array('style'=>'display:none;'),

			'template' => '{view}{update}{checkout}{checkin}',

				'buttons' => array(

					'update' => array(

						'visible'=>'$data->CHKFLAG=="Y" && $data->CHKOUTUSR==Yii::app()->user->name',

					),

						

					'checkout' => array(

						'label' => 'Check Out',

						 'click' => '$ajaxGridButtonFunction',

						'visible'=>'$data->CHKFLAG<>"Y"',

						//'url'=>'array("prjmaster/CheckOut","id"=>$data->ID,"Prjmaster_page"=>' . $page. ')',

						'imageUrl' => 'images/lock.jpg',

						),


						'checkin' => array(

								'label' => 'Check In',

								 'click' => '$ajaxGridButtonFunction',

								'visible'=>'$data->CHKFLAG=="Y" && $data->CHKOUTUSR == Yii::app()->user->name',

							//	'url'=>'array("prjmaster/CheckIn","id"=>$data->ID,"Prjmaster_page"=>' . $page. ')',

							'imageUrl' => 'images/unlock.jpg',

						),

				),

		),



Firebug is your friend!

Make sure there are no errors in the console when you load the page.

Check in the page source code that the click function is actually assigned.

Check at the end of the source code that the yiigridview function is properly called.

Check in the console whether clicking on the buttons does actually fire any ajax requests.

To my mind, if nothing is happening, then the function must be assigned to the buttons (otherwise the link would take effect, and you would navigate away from the page). Try adding a few console.alert in the function so that you can see in the console whether it is called. If so, then try manually, in the console, calling the $.fn.yiiGridView.update function to see what happens.

Ok, will do. Thanks

Try FireQuery:

https://addons.mozilla.org/en-US/firefox/addon/firequery/

This works for me …

I turned off ajax update …

*reference for future . yii users