Yii2 删除确认模式

Yii2 delete confirmation modal

我正在尝试使用 yii2 制作删除确认模式。 我有一个带有删除 gridview 项目的操作按钮的网格视图。

当用户单击此按钮时,会出现一个弹出模式,我无法获取必须删除的项目的 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。

希望它清楚,您将能够帮助我! 谢谢

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);
});
});"); 
?>

是否可以将id放入模型url类似于:

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

我自己找到了解决方案,感谢@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;
            });
        });
    });"
);

如果您有比我的答案更好的解决方案,请不要犹豫告诉我!

感谢大家的帮助:)

看来 Yii 已经有 javascript 用于提示确认对话框和 PHP Helper button/link。

参考:Simple analysis of post requests implemented with yii helpers Html class and yii.js of yii2

粘贴下面的代码以供快速参考:

<?= Html::a(
      'delete',
      [
          'delete',
          'id' => $id,
      ],
      [
          'data' => [
              'confirm' => 'Are you sure you want to delete it?',
              'method' => 'post',
          ],
      ]  ) 
?>