Yii 2: Pjax + Gridview delete 不发送 ajax 请求

Yii 2: Pjax + Gridview delete does not send ajax request

我正在将 Pjax 与 Gridview 一起使用,我希望我的所有操作按钮都执行 ajax。默认情况下,他们没有,所以我用谷歌搜索并找到了删除 data-pjax = 0 的方法。但是仍然没有 ajax 请求,所有请求都是常规请求。

很多人遇到这个问题,我也找不到解决方案。

我关注了:

我的代码:

<?php Pjax::begin(['id' => 'employee-timesheet-grid-id', 'timeout' => false, 'enablePushState' => false, 'clientOptions' => ['method' => 'POST']]) ?>
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'label' => 'Employee',
            'value' => function ($model) { 
                return $model->employeePayRate->employeeName; 
            },
        ],
        [
            'class' => 'yii\grid\ActionColumn', 
            'template' => '{view} {delete}',
            'buttons' => [
                'delete' => function ($url , $model) {
                    return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, 
                        ['data-confirm' => 'Are you sure you want to delete this item?', 'data-method' =>'POST'] );
                }
            ],
            'urlCreator' => function ($action, $model, $key, $index) {
                if ($action === 'view') {
                    $url = Url::to(['employee-time-sheet/view', 'id' => $model->id]);
                    return $url;
                } else if ($action === 'delete') {
                    $url = Url::to(['employee-time-sheet/delete', 'id' => $model->id]);
                    return $url;
                }
            }
        ],
    ],
]); ?>
<?php Pjax::end(); ?>

有没有人找到这个问题的解决方案?

试试这个。 ( 我的工作代码 )

首先在 Gridview 视图文件中跟随 JavaScript 注册 我在这里使用 bootbox 确认框。

$this->registerJs("

$(document).on('ready pjax:success', function () {
  $('.ajaxDelete').on('click', function (e) {
    e.preventDefault();
    var deleteUrl     = $(this).attr('delete-url');
    var pjaxContainer = $(this).attr('pjax-container');
    bootbox.confirm('Are you sure you want to change status of this item?',
            function (result) {
              if (result) {
                $.ajax({
                  url:   deleteUrl,
                  type:  'post',
                  error: function (xhr, status, error) {
                    alert('There was an error with your request.' 
                          + xhr.responseText);
                  }
                }).done(function (data) {
                  $.pjax.reload({container: '#' + $.trim(pjaxContainer)});
                });
              }
            }
    );
  });
});

");

下面是 Gridview

的代码
    <?php
\yii\widgets\Pjax::begin([
    'id' => 'pjax-list',
]); ?>
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel'  => $searchModel,
    'columns'      => [
        ['class' => 'yii\grid\SerialColumn'],
        'stu_category_name',
        [
            'class'    => 'yii\grid\ActionColumn',
            'template' => '{view} {delete}',
            'buttons'  => [
                'view'   => function ($url, $model) {
                    return ((Yii::$app->user->can("/student/stu/view"))
                        ? Html::a(
                            '<span class="glyphicon glyphicon-eye-open"></span>',
                            $url,
                            ['title' => Yii::t('app', 'View'),]
                        )
                        : '');
                },
                'delete' => function ($url, $model) {
                    return ((Yii::$app->user->can("/student/stu/delete"))
                        ? Html::a(
                            '<span class="glyphicon glyphicon-trash"></span>',
                            false,
                            [
                                'class'          => 'ajaxDelete',
                                'delete-url'     => $url,
                                'pjax-container' => 'pjax-list',
                                'title'          => Yii::t('app', 'Delete')
                            ]
                        )
                        : '');
                }
            ],
        ],
    ],
]); ?>
<?php \yii\widgets\Pjax::end(); ?>

感谢 @skworden 支持此解决方案。 此解决方案的 Yii 论坛 link。 click here

不要设置 data-methoddata-confirm 因为 Pjax 不支持。

删除两者后仍然无法正常工作,是的,因为您的控制器的以下代码不允许 Pjax 获取请求。

return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'], // **remove this**
                ],
            ],
        ];

你需要使用Pjax Post方法 在你的 Pjax 中应用这个

'clientOptions' => ['method' => 'POST']

对于警报框,您需要做一些额外的事情

完整的操作方法。

第 1 页。包含网格视图

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'layout' => "{pager}\n{summary}\n{items}\n{pager}",
        'columns' => [
            ['class' => 'yii\grid\CheckboxColumn'],
            '_id',
            'title',
            'notification:ntext',
            'date',
            ['class' => 'yii\grid\ActionColumn',
             'template' => '{view} {feedback}',
             'buttons' =>  ['feedback' => function ($url, $model, $key) {
                                return  Html::a('<i class="glyphicon glyphicon-comment"></i>'.$model->totalfeedback,'#');
                            },
                            'view' => function($url,$model,$key){

                                return $this->render('_viewLink',['model'=>$model]);

                        },
                            ],
             ]

            ],
    ]); ?>

第 2 页。Conatin link 和 Pjax 对于每个 link 查看、编辑、删除

echo Html::a('<span class="glyphicon glyphicon-eye-open"></span>',URL::to(['view','id'=>$model->_id]),['id' => 'view_link']);
Pjax::widget(['id'=>'view_member', 'linkSelector' => '#view_link','options'=>['tag'=>'span']]);
echo '&nbsp';
echo Html::a('<span class="glyphicon glyphicon-pencil"></span>',URL::to(['update','id'=>$model->_id]),['id' => 'edit_link']);
Pjax::widget(['id'=>'view_member', 'linkSelector' => '#edit_link','options'=>['tag'=>'span']]);
echo '&nbsp';
echo Html::a('<span class="glyphicon glyphicon-trash"></span>',
           URL::to(['delete','id'=>$model->_id]),
           ['id' => 'delete_link']);
Pjax::widget(['id'=>'view_member', 'linkSelector' => '#delete_link',
'options'=>['tag'=>'span'],'clientOptions' => ['method' => 'POST']]);