1

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 (!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 :)

Kai
  • 38,985
  • 14
  • 88
  • 103
Epicurus
  • 2,103
  • 7
  • 29
  • 39
  • Please just edit your question to include clarification. Answers are reserved for just that, answers. – Tim Post Mar 18 '11 at 12:39
  • This works as you described by default (version 1.1.12). If you look closely it actually does no redirection because its an ajax call and therefore won't pass the condition check: `!isset($_GET['ajax'])`. CGridView update is called later by javascript if deletion returns success. – Imre L Oct 30 '12 at 16:33

2 Answers2

1

You can set the return url via the CHtml::link call. Here is an example using delete

CHtml::link(
    'Delete',
    '#',
     array('submit'=>array('delete','id'=>$model->id),
           'params'=>('returnUrl'=>'controller/action...'), 
           'confirm' => 'Are you sure?'
     )
);

Pulled from this Stackoverflow answer.

Community
  • 1
  • 1
Jason George
  • 6,992
  • 8
  • 44
  • 60
1

The 'returnUrl' code you are looking at uses a POST variable for the returnUrl. To use this, you will need to POST that somehow. On the View this code is called from I am assuming there is a <input name="returnUrl"> field in the form. You should make sure this field (populated with the correct URL value) is on all of the Views you are POSTing from in order to access that POST variable in your Controller action.

If you are POSTing to the deleteAction via AJAX, I think you can set the $_POST['returnUrl'] variable with the jQuery AJAX function.

Another way to go might be to use CWebUser's returnUrl SESSION variable instead of this POST variable. I have never done this, but it's built in to Yii so I assume it works OK.

I never really liked the hacky $_POST['returnUrl'] that Gii generates anyway.

ANOTHER thing you could do, possibly, is look at the $_SERVER['HTTP_REFERER'] variable, and use that for the return redirect in your deleteAction. I don't know if that will be set correctly though, with complications from the 302 redirect/rewrites that Yii does.

Good luck!

thaddeusmt
  • 15,410
  • 9
  • 67
  • 67
  • Thanks for the reply. I was also considering using the AJAX option you mentioned.. The only problem is that I have no AJAX/jQuery experience whatsoever. – Epicurus Mar 18 '11 at 09:23