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

时间:2015-06-24 23:07:55

标签: gridview yii2 pjax

我正在使用Pjax和Gridview,我希望我的所有操作按钮都能执行ajax。默认情况下,他们不这样做,所以我用google搜索并找到了删除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(); ?>

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

2 个答案:

答案 0 :(得分:2)

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

首先在上述JavaScript观看文件中注册Gridview 在这里,我使用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论坛链接。 click here

答案 1 :(得分:0)

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

删除两个仍然不工作后,是的,因为你的控制器的下面的代码不允许Pjax得到请求。

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

您需要使用Pjax Post Method 在你的Pjax中应用它

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

对于Alert Box你需要做一些额外的事情

完整的方法如何做。

第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链接和Pjax用于每个链接查看,编辑,删除

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']]);
相关问题