如何触发Yii2确认对话框

时间:2016-02-16 18:25:28

标签: javascript php jquery yii2

我在ActionColumn上有自定义GridView并试图使用数据确认调用 yii.confirm 功能进行删除操作但对话框没有显示。

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

但是,当我尝试添加链接时没有下拉列表时,它可以正常工作

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

3 个答案:

答案 0 :(得分:8)

您可以添加此类链接

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

答案 1 :(得分:1)

我应该使用linkOtions表单项

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

答案 2 :(得分:1)

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

Use 'format'=>'raw' instead.