Yii2删除确认模式

时间:2016-05-09 08:08:40

标签: php yii2 yii2-basic-app

我试图用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时需要它,因为动作&#39; actionDelete&#39;需要物品的ID。

希望很清楚,你能够帮助我! 感谢

4 个答案:

答案 0 :(得分: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);
});
});"); 
?>

答案 1 :(得分: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',
    ]);
},

我的网址创建者:

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

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

感谢大家的帮助:)。

答案 2 :(得分:0)

是否可以将id放在模型url中,类似于:

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

答案 3 :(得分:0)

看来 Yii 已经有提示确认对话框的 javascript 和按钮/链接的 PHP Helper。

参考: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',
          ],
      ]  ) 
?>