2016-05-09 121 views
1

我正在嘗試使用yii2進行刪除確認模式。 我有一個網格視圖與刪除GridView的項目的操作按鈕。Yii2刪除確認模式

當用戶點擊此按鈕時,彈出窗口模式顯示,我無法獲取必須刪除的項目的ID。

這裏我GridView控件的代碼(僅操作按鈕):

'buttons' => [ 
       'view' => function ($url, $model) { 
          return Html::a('', $url, ['class' => 'btn btn-success btn-xs glyphicon glyphicon-eye-open']); 
         }, 
      'edit' => function ($url, $model) { 
          if (Yii::$app->user->getIdGroupe() != 1) 
          { 
           return Html::a(''); 
          } 
          return Html::a('', $url, ['class' => 'btn btn-warning btn-xs glyphicon glyphicon-pencil']); 
         }, 
         'delete' => function ($url, $model) { 
          return Html::a('', $url, ['class' => 'btn btn-danger btn-xs glyphicon glyphicon-trash', 'data-toggle' => 'modal', 'data-target' => '#modal', 'data-id' => $model->idRessource, 'id' => 'popupModal']); 
         }, 
       ], 
'urlCreator' => function ($action, $model, $key, $index) { 
          if ($action == 'view') { 
           $url = Url::to(['/ressource/view', 'id' => $model->idRessource]); 
          } else if ($action == 'edit') { 
           $url = Url::to(['/ressource/edit', 'id' => $model->idRessource]); 
          } else { 
           $url = '#'; 
          } 
          return $url; 
        }, 

然後模態:

<?php $url = Url::to(['ressource/delete']); ?> 

<?php Modal::begin([ 
    'header' => '<h2 class="modal-title"></h2>', 
    'id'  => 'modal-delete', 
    'footer' => Html::a('Supprimer', $url, ['class' => 'btn btn-danger']), 
]); ?> 

<?= 'Etes vous sur de vouloir supprimer la ressource ...'; ?> 

<?php Modal::end(); ?> 

最後的javascript:

<?php 
$this->registerJs("$(function() { 
    $('#popupModal').click(function(e) { 
     e.preventDefault(); 
     $('#modal-delete').modal('show').find('.modal-body') 
     .load($('.modal-dialog')); 
     var modal = $(this); 
     var triggered = $(e.relatedTarget); 
     var id = triggered.data('id'); 
     $('.modal-title').text('Supprimer la ressource ' + id); 
    }); 
});"); ?> 

而且問題是我無法獲取該項目的ID,並且我在構建$ url時需要它,因爲操作'actionDelete'需要它的ID EM。

希望它是明確的,你將能夠幫助我! 感謝

+0

actionDelete()需要通過post方法進行編號。 –

+0

我使用我自己的'actionDelete()',但感謝您的建議! ;) –

回答

1

我發現我自己的解決方案和謝謝@曉鬆國所以這裏是完整的答案

我的刪除按鈕:

'delete' => function ($url, $model) { 
    return Html::a('', $url, [ 
     'class'  => 'btn btn-danger btn-xs glyphicon glyphicon-trash popup-modal', 
     'data-toggle' => 'modal', 
     'data-target' => '#modal', 
     'data-id'  => $model->idRessource, 
     'data-name' => $model->nomRessource, 
     'id'   => 'popupModal', 
    ]); 
}, 

我的網址製作:

'urlCreator'  => function ($action, $model, $key, $index) { 
    $url = Url::to(['/ressource/delete', 'id' => $model->idRessource]); 
    return $url; 
}, 

我的模式:

<?php Modal::begin([ 
    'header' => '<h2 class="modal-title"></h2>', 
    'id'  => 'modal-delete', 
    'footer' => Html::a('Supprimer', '', ['class' => 'btn btn-danger', 'id' => 'delete-confirm']), 
]); ?> 

<?= 'Etes vous sur de vouloir supprimer cette ressource ?'; ?> 

<?php Modal::end(); ?> 

終於JavaScript的:

<?php 
$this->registerJs(" 
    $(function() { 
     $('.popup-modal').click(function(e) { 
      e.preventDefault(); 
      var modal = $('#modal-delete').modal('show'); 
      modal.find('.modal-body').load($('.modal-dialog')); 
      var that = $(this); 
      var id = that.data('id'); 
      var name = that.data('name'); 
      modal.find('.modal-title').text('Supprimer la ressource \"' + name + '\"'); 

      $('#delete-confirm').click(function(e) { 
       e.preventDefault(); 
       window.location = 'delete?id='+id; 
      }); 
     }); 
    });" 
); 

如果你有比我的答案更好的解決方案,請不要猶豫,告訴我!

感謝大家的幫助:

+0

默認情況下,'delete'操作只接受'post'方法。 ''。使用這些由GridView創建的代碼。 –

2

PHP按鈕:

'delete' => function ($url, $model) { 
    return Html::a('', $url, [ 
     'class' => '... popup-modal', 
     'data-toggle' => 'modal', 
     'data-target' => '#modal', 
     'data-id' => $model->idRessource, 
     'id' => 'popupModal-'. $model->idRessource 
    ]); 
}, 

JS:

<?php 
$this->registerJs("$(function() { 
$('.popup-modal').click(function(e) { 
    e.preventDefault(); 
    var modal = $('#modal-delete').modal('show'); 
    modal.find('.modal-body').load($('.modal-dialog')); 
    var that = $(this); 
    var id = that.data('id'); 
    modal.find('.modal-title').text('Supprimer la ressource ' + id); 
}); 
});"); 
?> 
+0

感謝人,它的工作原理,但現在我有id(在JS)我怎樣才能得到它在PHP中? –

+0

我找到了解決方案,我會自己發佈完整的答案,非常感謝您的幫助我會引用你:) –

0

,纔有可能把ID類似模型的網址:

<?php $url = Url::to(['ressource/delete', 'id' => $model->id]); ?> 
+1

''id'=> $ model-> id]);' –

+0

我不能使用' $ model-> id'在匿名函數之外(我定義每個按鈕或urlCreator),這樣我的問題:/ –