2015-06-24 18 views
6

Tôi đang sử dụng Pjax với GridView và tôi muốn tất cả các nút hành động của tôi làm ajax. Theo mặc định, họ không, vì vậy tôi googled và tìm cách để loại bỏ dữ liệu-pjax = 0. Nhưng vẫn còn, không có yêu cầu ajax, tất cả chúng đều là yêu cầu thường xuyên.Yii 2: Pjax + Gridview xóa không gửi yêu cầu ajax

Rất nhiều người gặp sự cố này và tôi cũng không thể tìm được giải pháp.

Tôi đã theo:

Mã của tôi:

<?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(); ?> 

Có ai tìm thấy giải pháp cho vấn đề này chưa?

+0

tôi đã làm giải pháp –

Trả lời

1

Hãy thử điều này. (mã làm việc của tôi)

đăng ký đầu tiên sau JavaScript trên Gridview quan điểm nêu trên nộp Ở đây tôi sử dụng hộp bootbox xác nhận.

$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)}); 
       }); 
       } 
      } 
    ); 
    }); 
}); 

"); 

Và bên dưới mã cho 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(); ?> 

Nhờ @skworden để hỗ trợ cho giải pháp này. Liên kết diễn đàn Yii cho giải pháp này. click here

+0

công trình ví dụ của bạn tuyệt quá. cảm ơn vì đã chia sẻ –

0

Không đặt data-methoddata-confirmPjax không được hỗ trợ.

Sau khi xóa cả hai vẫn không hoạt động, có vì mã dưới đây của người kiểm soát của bạn không cho phép Pjax nhận Yêu cầu.

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

Bạn cần phải sử dụng Pjax bài viết Phương pháp Áp dụng này trong Pjax bạn

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

Đối với Báo Box Bạn cần phải làm một số công cụ bổ sung

Full Way Làm thế nào để Do.

trang 1. Đó Chứa Lưới Xem

<?= 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]); 

         }, 
          ], 
      ] 

      ], 
    ]); ?> 

Trang 2. Đó Conatin một liên kết và Pjax Đối với mỗi liên kết Xem, chỉnh sửa, xóa

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

Để cảnh báo, hãy tự mình làm một số việc khác. –

Các vấn đề liên quan