1

I'm trying to make a delete confirmation modal with yii2. I have a grid view with an action button which deletes item of gridview.

When a user clicks on this button, a popup modal shows up and I cannot get the id of the item which must be deleted.

Here the code of my gridview (only the action button):

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

Then the modal :

<?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(); ?>

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

And the problem is I can't get the id of the item and I need it when I build the $url because the action 'actionDelete' need the id of the item.

Hope it is clear and you'll be able to help me ! Thanks

2
  • actionDelete() requires id via post method. Commented May 9, 2016 at 9:13
  • I use my own actionDelete() but thanks for the advice ! ;) Commented May 9, 2016 at 11:45

4 Answers 4

2

PHP buttons:

'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);
});
});"); 
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man it works but now I have the id (in JS) how can I get it in PHP ?
I found the solution, I will post the full answer by myself, thanks a lot for your help I will quote you :)
2

I've found the solution by myself and thanks to @XiaosongGuo so here is the full answer

My delete button :

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

My url creator :

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

My modal :

<?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(); ?>

and finally the 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;
            });
        });
    });"
);

If you have better solutions than my answer please do not hesitate to tell me !

Thanks for the help everyone :)

1 Comment

By default the action 'delete' only accepts 'post' method. <a data-pjax="0" data-method="post" data-confirm="您确定要删除此项吗?" aria-label="删除" title="删除" href="URL HERE"><span class="glyphicon glyphicon-trash"></span></a>. Use these code that is creating by GridView.
0

is it possible to put the id in the model url similar to:

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

2 Comments

'id' => $model->id]);
I can't use $model->id outside anonymous functions (where I defined each button or the urlCreator) so that my problem :/
0

It appears Yii already has the javascript for prompting a confirmation dialogue box and PHP Helper for the button/link.

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

Pasting the code below for quick reference:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.