2015-06-24 49 views
6

我使用Pjax與Gridview,我希望我所有的操作按鈕做Ajax。默認情況下,他們沒有,所以我GOOGLE了,並找到了刪除數據的方式 - pjax = 0.但仍然沒有ajax請求,所有這些都是常規請求。Yii 2:Pjax + Gridview刪除不發送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(); ?> 

有沒有人發現這個問題解決了嗎?

+0

我已經做了解決方案 –

回答

1

試試這個。 (我的工作代碼

以下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

+0

你的榜樣工程大。感謝分享 –

0

請勿設置data-methoddata-confirm,因爲Pjax不支持。

刪除兩個仍然沒有工作後,是因爲你的控制器的下面的代碼不允許PJAX獲取請求。

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

您需要使用Pjax郵政法 在你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一個鏈接,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']]); 
+0

警惕自己做一些額外的東西。 –