0

In my Yii2 app I have a view (item/view) that shows images (Image model) that belong to that Item. It's a PHP/HTML page, no Yii2 widgets (GridView, DetailView, etc). I want to have a delete button below the image that will use the delete method on the ImageController.

Is there a built in way to create a delete button that will have a confirmation dialog and handle the pjax like GridView does? Or will I have to roll my own?

sharf
  • 2,123
  • 4
  • 24
  • 47
  • not clear if you are asking about a `DetailView` widget or `GridView` where you are displaying the image and want a button, or you dont have any of them and just want a button in a simple view that should act the same as a default `delete` button in the `view.php` file that is generated by the `CRUD` which asks for a confirmation before delete. – Muhammad Omer Aslam Nov 25 '18 at 13:56
  • adding your view code or an image which describes the situation would help – Muhammad Omer Aslam Nov 25 '18 at 13:59
  • @MuhammadOmerAslam I updated my question. I don't have any widgets, just a plain view right now. I want to know if there's a built in way to replicate the gridview delete functionality. – sharf Nov 25 '18 at 15:03

1 Answers1

1

you can always use the data-confirm attribute to add the delete confirmation like in the grid view, also you should specify the 'method'=>'post' for the button you are creating see below

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

Apart from this if you want to change the default confirmation overall on the site to a more hify confirmation like sweet-alert you can see my answer here

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
  • Perfect! that worked wonderfully. I had no idea about those data attributes. I combined that with Kartik's dialog override and got the exact solution I was looking for, thanks! – sharf Nov 25 '18 at 16:40