Yii2 Pjax GridView action buttons issue

13,495

You must do like below:

For Delete Action

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- In your grid view:

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

Now, it works with Pjax.

Notes

  • 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
Share:
13,495
user3658609
Author by

user3658609

Updated on June 13, 2022

Comments

  • user3658609
    user3658609 almost 2 years

    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 yii\widgets\Pjax::begin(['id' => 'demo']); ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
    
            'id',
            'name',
    
    
            ['class' => 'yii\grid\ActionColumn'],
        ],
    
    ]); ?>
    <?php yii\widgets\Pjax::end(); ?>
    

    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.

  • user3658609
    user3658609 over 9 years
    In the delete action you are not deleting anything?
  • Michael Nguyen
    Michael Nguyen almost 9 years
    he does with $this->findModel($id)->delete();
  • Bloodhound
    Bloodhound over 8 years
    hi, it is giving me an 405 (Method Not Allowed) error
  • Gianpaolo Scrigna
    Gianpaolo Scrigna over 7 years
    It gives you "405 Method not allowed" because a actionDelete need to be a POST request. So you need to add 'data-method' => 'post' just after 'data-pjax'=>'w0' in the gridview button options. Look at the framework documentation
  • Admin
    Admin over 3 years
    @user3658609 no sense to spend time helping this person - he is responsive when help is needed but leaves most of question without feedback. Lots of unsolved questions.