我正在使用 yii2mod/yii2-sweet-alert在我的项目中,我在基础和高级主题上使用它,我喜欢它。
问题。 我怎样才能更改网格默认确认对话框,这是一个普通的 javascript 确认,以便使用 Sweet-alert让它看起来更好?
我已经尝试修改删除按钮的模板,因为如果你想更改消息,你将执行以下操作:
[
'class' => ActionColumn::className(),
'template' => '{update}{delete}',
'buttons' => [
'delete' => function($url, $model){
return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['delete', 'id' => $model->id], [
'class' => '',
'data' => [
'confirm' => 'Are you absolutely sure ? You will lose all the information about this user with this action.',
'method' => 'post',
],
]);
}
]
]
但我没有成功将确认消息从 javascript 更改为甜蜜警报。
此外,我正在尝试作为第二个选项,使其与 Krajee/ Grid and actionColumn 一起使用但仍然可以让它工作 «这是第二个正在工作的选项,来做到这一点»。
[
'class' => 'kartik\grid\ActionColumn',
'viewOptions' => ['hidden' => true],
'updateOptions' => ['title' => 'Edit events', 'data-toggle' => '??'],
'deleteOptions' => ['title' => 'delete your event', 'data-toggle' => 'I am Lost here'],
],
请问如何改变这种行为?
更多关于如何解决的信息 - 感谢 @muhammad-omer-aslam
在我的例子中,在您的公用文件夹中创建一个 js 文件
/backend/web/js/confirmSwal.js
并添加提供的代码:添加这些行
yii.confirm = function (message, okCallback, cancelCallback) { swal({ title: message, type: 'warning', showCancelButton: true, closeOnConfirm: true, allowOutsideClick: true }, okCallback); };
将此添加到您的 AppAssets 上
/backend/assets/AppAssets.php
public $js = [ '/js/confirmSwal.js', ];
就是这样,它的效果很漂亮。
再次感谢穆罕默德。
最佳答案
更新 2
只需更正您需要做的代码
okCallback.call()
或添加括号()
如 'okCallback()`- 然后是
cancelCallback.call()
或添加括号cancelCallback()
在 .then((selection)=>{})
里面,它是一个匿名函数,需要调用而不是仅仅使用 okCallback
所以里面的代码.then((selection)=>{})
变成了
if(selection){ okCallback.call();}else{ cancelCallback.call();}
更新
sweetalert 2.0
版本中不推荐使用以下选项,
回调
支持promise
: 如果用户单击确认按钮, promise 将解析为true
。如果警报被解除(通过在警报外部单击), promise 将解析为null
。
为清楚起见,allowClickOutside
现在是closeOnClickOutside
。
不再需要 showCancelButton
和showConfirmButton
。相反,您可以设置buttons: true
以显示两个按钮或设置buttons: false
以隐藏所有按钮。默认情况下,仅显示确认按钮。- 当使用单个字符串参数时(例如
swal("Hello world!")
),该参数将是模式的text
而不是它的title
。 type
和imageUrl
已替换为单个icon
选项。如果您使用的是简写版本 (swal("Hi", "Hello world", "warning")
),则无需更改任何内容。
因此,如果您使用 2.x
版本或从 1.x
升级,您可以将代码更改为以下内容。
yii.confirm = function (message, okCallback, cancelCallback) {
swal({
text: message,
icon: 'warning',
buttons : {
cancel : {
text : "Oops! No",
value : null,
visible : true,
className : "",
closeModal : true
},
confirm : {
text : "Delete It Already",
value : true,
visible : true,
className : "",
closeModal : true
}
},
closeOnClickOutside: true
}).then((selection) => {
if(selection){okCallback;}else{cancelCallback;}
});
}
您可以使用以下代码覆盖 Yii2 默认的 data-confirm
弹出窗口:
基础是包含 Assets ,然后添加这个 JS:
/**
* Override the default yii confirm dialog. This function is
* called by yii when a confirmation is requested.
*
* @param message the message to display
* @param okCallback triggered when confirmation is true
* @param cancelCallback callback triggered when canceled
*/
yii.confirm = function (message, okCallback, cancelCallback) {
swal({
title: message,
type: 'warning',
showCancelButton: true,
closeOnConfirm: true,
allowOutsideClick: true
}, okCallback);
};
关于php - Yii2:用 Sweet alert 替换 Gridview 使用的默认确认消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49220106/