2016-05-09 13 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を取得することはできませんし、アクション 'actionDelete'がそのIDを必要とするため、私は$ URLを構築するときに必要ですem。

希望があり、あなたが私を助けることができますように! おかげ

+0

actionDelete()にはpostメソッド経由のIDが必要です。 –

+0

私は自分の 'actionDelete()'を使っていますが、アドバイスに感謝します! ;) –

答えて

1

私が見つけました解決策は、自分自身と@ XiaosongGuoのおかげですので、ここでは完全な答えです

マイ削除ボタン:

'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', 
    ]); 
}, 

私のURLの作成者:

'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'メソッドしか受け付けないように、各ボタンまたはurlCreatorを定義した匿名関数の外で$ model-> id'を使用します。 '' 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

に似たモデルのURLにIDを入れすることが可能です:

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

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

+0

私は '私の問題:/ –