Yii2 Pjax GridView 操作按钮问题 [英] Yii2 Pjax GridView action buttons issue

查看:29
本文介绍了Yii2 Pjax GridView 操作按钮问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Pjax 制作 Ajax GridView.除了视图、更新和删除按钮不是 AJAX 外,一切正常.代码是:

I am trying to make an Ajax GridView using Pjax. Everything is working fine except the view, update and delete buttons are not AJAX. The code is:

<?php yiiwidgetsPjax::begin(['id' => 'demo']); ?>
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yiigridSerialColumn'],

        'id',
        'name',


        ['class' => 'yiigridActionColumn'],
    ],

]); ?>
<?php yiiwidgetsPjax::end(); ?>

问题是删除、查看和更新​​链接的属性 data-pjax=0 禁用了 AJAX 功能.我也找不到如何设置它 data-pjax=1.

The problem is that the links for delete, view and update have the attribute data-pjax=0 which disables AJAX functionality. I cant find out how to set it too data-pjax=1.

推荐答案

你必须像下面这样:

对于删除操作

1- 更改您的删除操作,如下所示:

1- Change your delete action like below:

public function actionDelete($id) {
    $this->findModel($id)->delete();
    if (Yii::$app->getRequest()->isAjax) {
        $dataProvider = new ActiveDataProvider([
            'query' => ModelName::find(),
            'sort' => false
        ]);
        return $this->renderPartial('index', [
                    'dataProvider' => $dataProvider
        ]);
    }
    return $this->redirect(['index']);
}

2- 在您的网格视图中:

2- In your grid view:

['class' => 'yiigridActionColumn',
        'buttons' => [
            'delete' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                            'title' => Yii::t('yii', 'Delete'),
                            'data-pjax'=>'w0',
                ]);
            }
        ]
    ],

现在,它适用于 Pjax.

注意事项

  • 我在 deleteAction() 中的代码可能会降低性能.你可以自己写.
  • w0 通常是 PJax 的默认 id.您可以将 id 添加到 PJax 并在那里写入.
  • 这对于UpdateView 来说是一样的,但是你需要改变你的updateview 视图.
  • 强烈推荐查看Yii2的官方PJax文档:http://www.yiiframework.com/doc-2.0/yii-widgets-pjax.html
  • My code in deleteAction() may decrease performance. You can write your own.
  • w0 usually is the default id of PJax. You can add an id to PJax and write it there instead.
  • This is the same for Update and View, But you need to change the way you show your update and view views.
  • This is highly recommended to take a look at Yii2's official PJax document: http://www.yiiframework.com/doc-2.0/yii-widgets-pjax.html

这篇关于Yii2 Pjax GridView 操作按钮问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆