Yii2: Replace default confirmation message used by Gridview with Sweet alert

♀尐吖头ヾ 提交于 2019-12-01 20:37:27

UPDATE2

Just correction in the code you need to do

  • okCallback.call() or add parenthesis () like 'okCallback()`
  • and then cancelCallback.call() or add parenthesis cancelCallback()

inside the .then((selection)=>{}) and it is an anonymous function and needs to be called rather than just using okCallback so the code inside the .then((selection)=>{}) becomes

if(selection){ okCallback.call();}else{ cancelCallback.call();}

UPDATE

The following options are deprecated in sweetalert 2.0 version,

  • callback in favor of promise: If the user clicks the confirm button, the promise resolves to true. If the alert is dismissed (by clicking outside of it), the promise resolves to null.

  • allowClickOutside is now closeOnClickOutside for clarity.

  • showCancelButton and showConfirmButton are no longer needed. Instead, you can set buttons: true to show both buttons or buttons: false to hide all buttons. By default, only the confirm button is shown.
  • When using a single string parameter (e.g. swal("Hello world!")), that parameter will be the modal's text instead of its title.
  • type and imageUrl have been replaced with a single icon option. If you're using the shorthand version (swal("Hi", "Hello world", "warning")) you don't have to change anything.

So you can change the code to the following in case you are using ver 2.x or upgrading from 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;}
    });
}

You can override the Yii2 default data-confirm popup with the following code:

The basics are to include the asset, then add this 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);
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!