Setting returnURL for CButtonColumn button

Hey there,

I’m looking at the controller for the default Delete button in the CButtonColumn class. It manages to return to the previous web-page after deleting a CGridView line and remain on the same page of the CGridView, as opposed to going to the first page. The lines responsible for this in the associated controller seem to be:

// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

if (!isset($_GET['ajax']))

   $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

I would like to create a new custom button that has this behavior (i.e. returning to the previous view without resetting the pagination to page 1), but simply including the above lines of code in the button’s associated action does not do the trick. I think I need to send that ‘returnUrl’ parameter somehow, but I cannot figure out how :)


This line just returns to previous view (‘admin’)… to remember and return to the page jQuery is used in jquery.yiigridview.js…but it’s a custom solution for the grid/list view…


I read this three times and still don’t understand what you are trying to say. Could you expond. Thanks

there is nothing hard to understand…

the delete button of CGridView returns to a current page… and you would like to do the same for your button… but this function is implemented in the jQuery of the gridview…

the code that does that is not in the controller… in the controller you can have only $this->redirect(‘admin’)… the returnUrl has nothing to do with that…

I have just stumbled across this after reading this later post, so I though the same thing might answer your question.

As Mdomba said, the redirect in the controller action has nothing to do with this behaviour - it is all in the jQuery.

To get the same behaviour for your custom buttons, you will need to make their ajax calls through the $.fn.yiiGridView.update function rather than directly, adding a success callback that makes the grid update. Basically just copy the jQuery code for the delete button from the CButtonColumn class. And if you need control over the options in the ajax call, don’t worry - you can specify them in the update options map - the update function simply has defaults which are then overridden by merging with the options map.

I’ve just remembered things like javascript not being activated - so if javascript is not activated then it is indeed the redirect that has the desired effect (and should probably redirect to the referring url).

It should be noted though, that usually this does not apply to the delete button, since the delete button in the GridView will not work by default if javascript is disabled since we usually make a check to force post requests in the delete action.