1

我正在查看 CButtonColumn 类中默认删除按钮的控制器。它在删除 CGridView 行后设法返回到上一个网页并保持在 CGridView 的同一页面上,而不是转到第一页。相关控制器中负责此操作的行似乎是:

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

我想创建一个具有此行为的新自定义按钮(即返回上一个视图而不将分页重置为第 1 页),但仅在按钮的关联操作中包含上述代码行并不能解决问题。我想我需要以某种方式发送那个'returnUrl'参数,但我不知道如何:)

4

2 回答 2

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!

于 2011-03-16T15:07:12.047 回答
1

您可以通过 CHtml::link 调用设置返回 url。这是一个使用删除的例子

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

这个Stackoverflow 答案中提取。

于 2012-09-14T18:35:45.030 回答