How to trigger Yii2 Confirm Dialog

19,227

Solution 1

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

Solution 2

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

Solution 3

That's because you are using 'format'=>'html' option. The html formatter uses HTMLPurifier, that get rid of data attributes.

Use 'format'=>'raw' instead.

Share:
19,227
Admin
Author by

Admin

Updated on June 23, 2022

Comments

  • Admin
    Admin almost 2 years

    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;
        },
    ],
    
  • Admin
    Admin about 8 years
    Hi thanks for the improvement, but the confirmation dialog still not appears
  • ScaisEdge
    ScaisEdge about 8 years
    Please show the all the code of actioncolumn section in grdiview .. the part you show in notb enough for a better evaluation..
  • vanarie
    vanarie over 6 years
    The solution does work if you remove the square brackets surrounding the data confirm array, so it's: 'data' => [ 'confirm' => 'Are you sure ?', 'method' => 'post', ]