davidxu/yii2-sweetalert2-widget
Composer 安装命令:
composer require davidxu/yii2-sweetalert2-widget
包简介
Renders a SweetAlert2 widget for Yii2.
README 文档
README
Renders a SweetAlert2 widget for Yii2.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require davidxu/yii2-sweetalert2-widget "^2.0"
or add
"davidxu/yii2-sweetalert2-widget": "^2.0"
to the require section of your composer.json file.
Usage
Once the extension is installed, simply use it in your code by:
Flash message
View:
<?= \davidxu\sweetalert2\SweetAlert2::widget() ?>
Controller:
<?php
Yii::$app->session->setFlash('', [
[
'title' => 'Auto close alert!',
'text' => 'I will close in 2 seconds.',
'timer' => 2000,
],
[
'callback' => new \yii\web\JsExpression("
function (result) {
// handle dismiss, result.dismiss can be 'cancel', 'overlay', 'close', and 'timer'
if (result.isConfirmed) {
return true;
}
}
"),
],
]);
Render Widget
View:
<?php
use davidxu\sweetalert2\SweetAlert2;
A message with auto close timer
<?= SweetAlert2::widget([
'options' => [
'title' => 'Auto close alert!',
'text' => 'I will close in 2 seconds.',
'timer' => 2000,
],
'callback' => new \yii\web\JsExpression("
function (result) {
if (result.dismiss === 'timer') {
console.log('I was closed by the timer')
}
}
"),
]) ?>
Custom HTML description and buttons
<?= SweetAlert2::widget([
'options' => [
'title' => '<i>HTML</i> <u>example</u>',
'icon' => 'success,
'html' => 'You can use <b>bold text</b>,'
. '<a href="//github.com">links</a> '
. 'and other HTML tags',
'showCloseButton' => true,
'showCancelButton' => true,
'confirmButtonText' => '<i class="fa fa-thumbs-up"></i> Great!',
'cancelButtonText' => '<i class="fa fa-thumbs-down"></i>',
],
]) ?>
A warning message, with a function attached to the "Confirm"-button...
<?= SweetAlert2::widget([
'options' => [
'title' => 'Are you sure?',
'text' => "You won't be able to revert this!",
'icon' =>'warning',
'showCancelButton' => true,
'confirmButtonColor' => '#3085d6',
'cancelButtonColor' => '#d33',
'confirmButtonText' => 'Yes, delete it!',
],
'callback' => new \yii\web\JsExpression("
function (result) {
if(result.value === true){
swal.fire('Deleted!','Your file has been deleted.','success')
}
}
"),
]) ?>
... and by passing a parameter, you can execute something else for "Cancel".
<?= SweetAlert2::widget([
'options' => [
'title' => 'Are you sure?',
'text' => "You won't be able to revert this!",
'icon' =>'warning',
'showCancelButton' => true,
'confirmButtonColor' => '#3085d6',
'cancelButtonColor' => '#d33',
'confirmButtonText' => 'Yes, delete it!',
'cancelButtonText' => 'No, cancel!',
'confirmButtonClass' => 'btn btn-success',
'cancelButtonClass' => 'btn btn-danger',
'buttonsStyling' => false,
],
'callback' => new \yii\web\JsExpression("
function (result) {
if(result.value) {
Swal.fire('Deleted!','Your file has been deleted.','success')
}
if (result.dismiss === 'cancel') {
Swal.fire(
'Cancelled',
'Your imaginary file is safe :)',
'error'
)
}
}
"),
]) ?>
Input Types Example
Text:
<?= SweetAlert2::widget([
'options' => [
'title' => 'Input something',
'input' => 'text',
'showCancelButton' => true,
'inputValidator' => new \yii\web\JsExpression("
function (value) {
return new Promise(function (resolve) {
if (value) {
resolve()
} else {
resolve('You need to write something!')
}
})
}
")
],
'callback' => new \yii\web\JsExpression("
function (result) {
if(result.value) {
Swal.fire({
type: 'success',
html: 'You entered: ' + result.value
})
}
}
"),
]) ?>
Email:
<?= SweetAlert2::widget([
'options' => [
'title' => 'Input email address',
'input' => 'email',
],
'callback' => new \yii\web\JsExpression("
function (result) {
Swal.fire({
type: 'success',
html: 'Entered email: ' + result.value
})
}
"),
]) ?>
Password:
<?= SweetAlert2::widget([
'options' => [
'title' => 'Enter your password',
'input' => 'password',
'inputAttributes' => [
'maxlength' => 10,
'autocapitalize' => 'off',
'autocorrect' => 'off',
]
],
'callback' => new \yii\web\JsExpression("
function (result) {
if (result.value) {
Swal.fire({
type: 'success',
html: 'Entered password: ' + result.value
})
}
}
"),
]) ?>
Textarea:
<?= SweetAlert2::widget([
'options' => [
'input' => 'textarea',
'showCancelButton' => true,
],
'callback' => new \yii\web\JsExpression("
function (result) {
if (result.value) {
Swal.fire(result.value)
}
}
"),
]) ?>
Select:
<?= SweetAlert2::widget([
'options' => [
'title' => 'Select Country',
'input' => 'select',
'inputOptions' => [
'CHN' => 'China',
'RUS' => 'Russia',
'USA' => 'America',
],
'inputPlaceholder' => 'Select country',
'showCancelButton' => true,
'inputValidator' => new \yii\web\JsExpression("
function (value) {
return new Promise(function (resolve) {
if (value === 'CHN') {
resolve()
} else {
resolve('You need to select CHN :)')
}
})
}
")
],
'callback' => new \yii\web\JsExpression("
function (result) {
if (result.value) {
Swal.fire({
type: 'success',
html: 'You selected: ' + result.value
})
}
}
"),
]) ?>
Radio:
<?php
$script = new \yii\web\JsExpression("
// inputOptions can be an object or Promise
var inputOptions = new Promise(function (resolve) {
setTimeout(function () {
resolve({
'#ff0000': 'Red',
'#00ff00': 'Green',
'#0000ff': 'Blue'
})
}, 2000)
})
");
$this->registerJs($script, \yii\web\View::POS_HEAD);
echo SweetAlert2::widget([
'options' => [
'title' => 'Select color',
'input' => 'radio',
'inputOptions' => new \yii\web\JsExpression("inputOptions"),
'inputValidator' => new \yii\web\JsExpression("
function (result) {
return new Promise(function (resolve) {
if (result) {
resolve()
} else {
resolve('You need to select something!')
}
})
}
")
],
'callback' => new \yii\web\JsExpression("
function (result) {
Swal.fire({
type: 'success',
html: 'You selected: ' + result.value
})
}
"),
]); ?>
Checkbox:
<?= SweetAlert2::widget([
'options' => [
'title' => 'Terms and conditions',
'input' => 'checkbox',
'inputValue' => 1,
'inputPlaceholder' => 'I agree with the terms and conditions',
'confirmButtonText' => 'Continue <span class="glyphicon glyphicon-arrow-right" aria-hidden="true"></span>',
'inputValidator' => new \yii\web\JsExpression("
function (result) {
return new Promise(function (resolve) {
if (result) {
resolve()
} else {
resolve('You need to agree with T&C')
}
})
}
")
],
'callback' => new \yii\web\JsExpression("
function (result) {
Swal.fire({
type: 'success',
html: 'You agreed with T&C :' + result.value
})
}
"),
]) ?>
File:
<?= SweetAlert2::widget([
'options' => [
'title' => 'Select image',
'input' => 'file',
'inputAttributes' => [
'accept' => 'image/*',
'aria-label' => 'Upload your profile picture',
],
],
'callback' => new \yii\web\JsExpression("
function(result) {
var reader = new FileReader
reader.onload = function (e) {
Swal.fire({
title: 'Your uploaded picture',
imageUrl: e.target.result,
imageAlt: 'The uploaded picture'
})
}
reader.readAsDataURL(result.value)
}
"),
]) ?>
Range:
<?= SweetAlert2::widget([
'options' => [
'title' => 'How old are you?',
'icon' => 'question',
'input' => 'range',
'inputAttributes' => [
'min' => 8,
'max' => 120,
'step' => 1,
],
'inputValue' => 25,
]
]) ?>
Multiple inputs aren't supported, you can achieve them by using html and preConfirm parameters.
Inside the preConfirm() function you can pass the custom result to the resolve() function as a parameter:
<?= SweetAlert2::widget([
'options' => [
'title' => 'Multiple inputs',
'html' => '<input id="swal-input1" class="swal2-input"> <input id="swal-input2" class="swal2-input">',
'preConfirm' => new \yii\web\JsExpression("
function () {
return new Promise(function (resolve) {
resolve([
$('#swal-input1').val(),
$('#swal-input2').val()
])
})
}
"),
'onOpen' => new \yii\web\JsExpression("
function () {
$('#swal-input1').focus()
}
"),
],
'callback' => new \yii\web\JsExpression("
function (result) {
Swal.fire(JSON.stringify(result.value))
}
"),
]) ?>
Ajax request example
<?= SweetAlert2::widget([
'options' => [
'title' => 'Submit email to run ajax request',
'input' => 'email',
'showCancelButton' => true,
'confirmButtonText' => 'Submit',
'showLoaderOnConfirm' => true,
'preConfirm' => new \yii\web\JsExpression("
function (email) {
return new Promise(function (resolve) {
setTimeout(function () {
if (email === 'taken@example.com') {
swal.showValidationError(
'This email is already taken.'
)
}
resolve()
}, 2000)
})
}
"),
'allowOutsideClick' => false,
],
'callback' => new \yii\web\JsExpression("
function (result) {
if (result.value) {
Swal.fire({
type: 'success',
title: 'Ajax request finished!',
html: 'Submitted email: ' + result.value
})
}
}
"),
]) ?>
More Information
Please, check the SweetAlert2
License
The MIT License (MIT). Please see License File for more information.
davidxu/yii2-sweetalert2-widget 适用场景与选型建议
davidxu/yii2-sweetalert2-widget 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「extension」 「widget」 「yii2」 「sweetalert2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 davidxu/yii2-sweetalert2-widget 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 davidxu/yii2-sweetalert2-widget 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 davidxu/yii2-sweetalert2-widget 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
Adds more BBCode
统计信息
- 总下载量: 29
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-17