我正在嘗試使用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。
希望它是明確的,你將能夠幫助我! 感謝
actionDelete()需要通過post方法進行編號。 –
我使用我自己的'actionDelete()',但感謝您的建議! ;) –