Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger Yii2 Confirm Dialog

I have a custom ActionColumn on GridView and trying to call yii.confirm function using data-confirm for delete action but the dialog not shown.

[
'format'=>'html',
'content'=>function($data) {
    $btn = ButtonDropdown::widget([
    'label' => 'Action',
    'options' => ['class'=>'btn btn-sm btn-primary dropdown-toggle', 'type'=>'button'],
    'dropdown' => [
        'options' => ['class'=>'dropdown-menu action', 'role'=>'menu'],
        'items' => [
            '<li><a href="'.Url::to(['details','id'=>$data->id]) .'"><i class="fa fa-pencil"></i> Details</a></li>',
            '<li><a href="'. Url::to(['edit', 'id' => $data->id]) .'"><i class="fa fa-eye"></i> Edit</a></li>',
            '<li role="presentation" class="divider"></li>',
            '<li><a data-method="post" data-confirm="Are you sure ?" href="'.Url::to(['delete', 'id' => $data->id]).'"><i class="fa fa-trash"></i> Delete</a></li>',                                                
        ],
    ],
]);
return $btn;
},
],

But when I'm trying to add link without dropdown it works

[
'format'=>'html',//raw, html
    'content'=>function($data) {
        $btn ='<a data-method="post" data-confirm="Are you sure ?" href="'.Url::to(['delete', 'id' => $data->id]).'"><i class="fa   fa-trash"></i> Delete</a>';                                                
        return $btn;
    },
],
like image 676
JosE Avatar asked Feb 16 '16 18:02

JosE


2 Answers

You can add a link like this

    <?php echo Html::a(Yii::t('backend', 'Delete'), ['delete', 'id' => $model->id], [
        'class' => 'btn btn-danger',
        'data' => [
            'confirm' => Yii::t('backend', 'Are you sure you want to delete this item?'),
            'method' => 'post',
        ],
    ]) ?>
like image 128
crafter Avatar answered Sep 20 '22 11:09

crafter


I should use linkOtions form Items

     [
    'format'=>'html',
    'content'=>function($data) {
        $btn = ButtonDropdown::widget([
        'label' => 'Action',
        'options' => ['class'=>'btn btn-sm btn-primary dropdown-toggle', 'type'=>'button'],
        'dropdown' => [
            'options' => ['class'=>'dropdown-menu action', 'role'=>'menu'],
            'items' => [
              ['label' => 'Details', 'url' =>  ['details','id'=>$data->id], 
                      'linkOptions' => ['class'=>'fa fa-pencil'],],
              ['label' => 'Edit', 'url' =>  ['edit','id'=>$data->id], 
                      'linkOptions' => ['class'=>'fa fa-eye'],],
              ['label' => '<span role="presentation" class="divider"></span>'],
              ['label' => 'Delete', 'url' =>  ['delete','id'=>$data->id], 
                      'linkOptions' => ['class'=>'fa fa-trash' , 'data' => [
                    'confirm' => 'Are you sure ?',
                    'method' => 'post',
                ]],],                 
            ],
        ],
    ]);
    return $btn;
  },
  ],
like image 28
ScaisEdge Avatar answered Sep 19 '22 11:09

ScaisEdge