Saturday, March 19, 2022

[FIXED] delete button with confirmation in yii2

Issue

This view.php is generated with CRUD in Yii2 but the delete button doesn't confirm window.

But in index.php generated with CRUD in Yii2, The confirm window for delete worked.

<p>
    <?= Html::a('Edit', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
    <?= Html::a('Delete', ['delete', 'id' => $model->id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
    ]) ?>
    <?= Html::a('Back','index', ['class' => 'btn btn-warning']) ?>
</p>

Delete Button in index.php:

<a href="var/delete?id=2" title="Delete" aria-label="Delete" data-pjax="0" data-confirm="Are you sure you want to delete this item?" data-method="post"><span class="glyphicon glyphicon-trash"></span></a>

Delete Button in view.php:

<a class="btn btn-danger" href="var/delete?id=2" data-confirm="Are you sure you want to delete this item?" data-method="post">Delete</a>

Solution

The section about yii.js is still in progress.

To get default support of confirmation you should register yii.js.

Using yii\web\YiiAsset

use yii\web\YiiAsset;
YiiAsset::register($this);

or registering script files (not recommend, just for example)

$this->registerJsFile('@yii/assets/yii.js', ['depends' => [\yii\web\JqueryAsset::className()]]);


Answered By - SiZE

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.