yii2 GridView + PJAX + ActionColumn自定义按钮

时间:2015-08-11 13:20:57

标签: ajax gridview yii2 pjax

好的,我现在试图解决这个问题好几个小时......非常困难

所以这是问题组合: GridView + PJAX + ActionColumn的自定义按钮

它正确地做了什么:

  • 点击
  • 请求Pjax
  • 通过ajax
  • 设置活动标志
  • 加载 新内容

不做的事情:

  • 保留网址 - 也就是ajax调用之后,链接中的网址已更改为“person / active / 123”而不是原来的“person / index”....

这非常有问题,因为如果用户现在搜索或订购列,则会再次触发actionActive。

我的想法:

  • 设置enablePushState = false 无法解决主要问题 - 浏览器窗口中的URL现在是正确的但是GridView中的URL链接仍然是“person / active / 123”

  • 在AJAX通话中覆盖Yii :: $ app-> request-url

=>没效果

use kartik\grid\GridView;
// VIEW 
[
'class'         => 'kartik\grid\ActionColumn',
'template'      => '{active} {view} {update} {delete}',      
'buttons'       => [
   "active" => function ($url, $model) {
        if ($model->active == 1) $icon = "pause";
        else $icon = "play";

        return Html::a(\kartik\helpers\Html::icon($icon), $url, [
        'title'              => Yii::t('app', 'Toogle Active'),
        'data-pjax'          => '1',
        'data-toggle-active' => $model->id
    ]);
   },
],
'urlCreator'    => function ($action, $model, $key, $index) {
    return \yii\helpers\Url::to(["person/" . $action, "id" => $model->id]);
},
'viewOptions'   => ['label' => \kartik\helpers\Html::icon("folder-open"), 'title' => Yii::t("app", 'View {modelClass}', ['modelClass' => 'Person']), 'data-toggle' => 'tooltip'],
'updateOptions' => ['title' => Yii::t("app", 'Update {modelClass}', ['modelClass' => 'Person']), 'data-toggle' => 'tooltip'],
'deleteOptions' => ['title' => Yii::t("app", 'Delete {modelClass}', ['modelClass' => 'Person']), 'data-toggle' => 'tooltip'],
],

GridView::widget([
    'dataProvider'     => $dataProvider,
    'filterModel'         => $searchModel,
    'columns'            => $gridColumns,
    'pjax'               => true,
])

// CONTROLLER
public function actionActive($id)
{
    $model = $this->findModel($id);
    if ($model->active == 0) $model->active = 1;
    else $model->active = 0;

    if (!$model->save()) {
        Yii::$app->session->addFlash("error", "Error saving");
    }
    $model->refresh();

    if (Yii::$app->request->isAjax) { // Render the index view
        return $this->actionIndex();
    }
    else return $this->redirect(['person/index']);
}

0 个答案:

没有答案