yii2 - 如何在 Gridview 中将 Pjax 与自定义按钮一起使用

标签 yii2 yii2-advanced-app pjax

这是我的gridview

<?php Pjax::begin(['id' => 'vouchers']) ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
        ['header'=>'Cancel Voucher',
            'value'=> function($data)
                    { 
                       return  Html::a(Yii::t('app', ' {modelClass}', [
                                'modelClass' =>"Cancel",
                            ]), ['generalentries/cancel_voucher','voucher_no'=>$data->voucher_no], 
                            ['class' => 'btn btn-xs btn-danger',
                            'onclick'=>"return confirm('Are you Sure you want to CANCEL this Voucher?');"]
                            );  
                    },
            'format' => 'raw'
            ],
        ],
    ]); ?><?php Pjax::end() ?>

这是我的cancel_voucher操作

public function actionCancel_voucher( $voucher_no )
    {
      $entries = TabGLEntry::find()->where(['voucher_no' => $voucher_no])->all();
      foreach($entries as $entry)
      {
        $entry->update_note  = FALSE;
        $entry->is_deleted = 1;
        $entry->save();
      }
      $this->redirect( Yii::$app->request->referrer );
    }

当我点击“取消”按钮时,如何使用 pjax 更新 GridView ?

我尝试为按钮提供一个 id,然后在按钮的 onClick 事件上,我尝试使用 $.pjax.reload({container:"#vouchers"}) 更新 gridview ;但是没用

编辑

这就是我尝试的方法

<?php 
        \yii\widgets\Pjax::begin([
        'id' => 'countries', 
        'timeout' => false, 
        // 'enablePushState' => false, 
        'clientOptions' => ['method' => 'POST']]) 
    ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

        ['header'=>'Cancel Voucher',
            'value'=> function($data)
                    { 
                       return  Html::a(Yii::t('app', ' {modelClass}', [
                                'modelClass' =>"Cancel",
                            ]), ['generalentries/cancel_voucher','voucher_no'=>$data->voucher_no], 
                            ['class' => 'btn btn-xs btn-danger','id' =>'cancel_voucher',
                            'onclick'=>"return confirm('Are you Sure you want to CANCEL this Voucher?');"]
                            );  
                    },
            'format' => 'raw'
            ],

<?php
$this->registerJs(
    'jQuery(document).ready(function($){
                $(document).ready(function () {
                    $("#cancel_voucher").on("click", function(){
                        alert("hai");
                        $.pjax.reload({container:"#countries"});
                    });
                });
            });'
            );
?>

Issue

最佳答案

您必须返回内容,该内容将显示在 Pajax block 中,而不是 $this->redirect( Yii::$app->request->referrer );

public function actionCancel_voucher( $voucher_no )
{
  $entries = TabGLEntry::find()->where(['voucher_no' => $voucher_no])->all();
  foreach($entries as $entry)
  {
    $entry->update_note  = FALSE;
    $entry->is_deleted = 1;
    $entry->save();
  }
 if (Yii::$app->getRequest()->getIsPjax()) {
    //render part of view with grid
    return $this->renderAjax($url, $params);
 }
 else { $this->redirect( Yii::$app->request->referrer ); }
}

为了正确的 pajax 重定向,您必须设置 X-Pjax-Url header 。

if (Yii::$app->getRequest()->getIsPjax()) {
        $response = Yii::$app->getResponse();
        $destination = Url::to($url);
        $response->getHeaders()->set('X-Pjax-Url', $destination);
        $response->getHeaders()->set('Location', $destination);
        return null;
    } 

关于yii2 - 如何在 Gridview 中将 Pjax 与自定义按钮一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34721300/

相关文章:

php - pjax 提交表单 URL 重定向

php - 在 Yii2 的 SqlDataProvider 中使用 SQL_CALC_FOUND_ROWS

php - Yii 2 用户身份验证和基于角色的访问控制(RBAC)模块安装和使用

javascript - Yii2 如何从特定 View 或 Controller 操作中包含多个 css 文件

yii - 如果 yii2 gridview 中的条件事件按钮或非事件按钮显示在 gridview 上,如何使用

ruby-on-rails - 我可以在Rails中使用Turbolink手动导航吗?

search - Yii2 修改 Model search() 中的 find() 方法

php - 如何在 Yii2 上显示 parent 的名字

php - 在 yii2 中保存多条记录

javascript - Backbone.js - 如何包含外部 HTML 片段