Last active
October 31, 2022 09:43
-
-
Save ssiva13/d9efe7bbef0114907067e8729734fb59 to your computer and use it in GitHub Desktop.
Override yii data-confirm dialog globally using npm sweet alert. ( after installing with composer require npm-asset/sweetalert2 ) -- (make sure you add the override.js file to your main AppAsset.php file)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Override the default yii and kartik confirm dialogs. This function is | |
* called by yii or kartik when a confirmation is requested. | |
* This overides to use the sweetalert2 javascript library. | |
* @param message the message to display | |
* @param okCallback triggered when confirmation is true | |
* @param cancelCallback callback triggered when cancelled | |
* @var krajeeYiiConfirm this variable overides the kartik dialog confirm, please ensure to use it as is. | |
* | |
* | |
* | |
* | |
* make sure you add the override.js file to your main AppAsset.php file | |
*/ | |
var krajeeYiiConfirm; | |
(function () { | |
"use strict"; | |
krajeeYiiConfirm = function() { | |
yii.confirm = function (message, okCallback, cancelCallback) { | |
Swal.fire({ | |
icon: 'warning', | |
html: message, | |
showCancelButton: true, | |
reverseButtons: true, | |
showConfirmButton: true, | |
confirmButtonColor: '#dc3545', | |
}).then(function (selection) { | |
if (selection.isConfirmed) { | |
!okCallback || okCallback(); | |
}else{ | |
!cancelCallback || cancelCallback(); | |
} | |
}); | |
}; | |
}; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Override the default yii confirm dialog. This function is | |
* called by yii when a confirmation is requested. | |
* This overides to use the sweetalert2 javascript library. | |
* @param message the message to display | |
* @param okCallback triggered when confirmation is true | |
* @param cancelCallback callback triggered when cancelled | |
* | |
* | |
* make sure you add the override.js file to your main AppAsset.php file | |
*/ | |
(function () { | |
"use strict"; | |
yii.confirm = function (message, okCallback, cancelCallback) { | |
Swal.fire({ | |
icon: 'warning', | |
html: message, | |
showCancelButton: true, | |
reverseButtons: true, | |
showConfirmButton: true, | |
confirmButtonColor: '#dc3545', | |
}).then(function (selection) { | |
if (selection.isConfirmed) { | |
!okCallback || okCallback(); | |
}else{ | |
!cancelCallback || cancelCallback(); | |
} | |
}); | |
}; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\assets; | |
use yii\web\AssetBundle; | |
class SweetAlertAsset extends AssetBundle | |
{ | |
public $sourcePath = '@npm/sweetalert2/dist'; | |
public $js = [ | |
'sweetalert2.min.js', | |
]; | |
public $css = [ | |
'sweetalert2.min.css', | |
]; | |
public $depends = [ | |
'kartik\dialog\DialogYiiAsset', | |
]; | |
} |
Author
ssiva13
commented
Mar 14, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment