PHPのAPI開発が進む中、フロントエンドでのデータ処理が一般的となり、ユーザー体験の向上が求められています。
swal("メッセージの表示")
swal("タイトルの表示", "補足説明の追加")
swal("完了しました!", "ボタンクリックありがとうございます", "success")
swal({
title: "実行しますか?",
text: "この仮想ファイルは復元できません!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "削除を実行",
closeOnConfirm: false
},
function(){
swal("削除完了!", "仮想ファイルを削除しました。", "success");
});
swal({
title: "実行しますか?",
text: "この仮想ファイルは復元できません!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "削除を実行",
cancelButtonText: "キャンセルしてください",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm){
if (isConfirm) {
swal("削除完了!", "仮想ファイルを削除しました。", "success");
} else {
swal("キャンセル", "仮想ファイルは安全です :)", "error");
}
});
swal({
title: "スウィート!",
text: "カスタム画像の表示",
imageUrl: "images/thumbs-up.jpg"
});
swal({
title: "HTML <small>タイトル</small>!",
text: "カスタム <span style="color:#F8BB86">HTML</span> メッセージ。",
html: true
});
swal({
title: "2秒後に閉じます",
text: "2秒後にこのポップアップが閉じます。",
timer: 2000,
showConfirmButton: false
});
swal({
title: "入力フォーム",
text: "興味深い内容を入力してください:",
type: "input",
showCancelButton: true,
closeOnConfirm: false,
animation: "slide-from-top",
inputPlaceholder: "入力してください"
},
function(inputValue){
if (inputValue === false) return false;
if (inputValue === "") {
swal.showInputError("入力必須です!");
return false
}
swal("入力完了!", "入力内容: " + inputValue, "success");
});
swal({
title: "AJAXリクエスト例",
text: "実行してAJAXリクエストを送信",
type: "info",
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true,
},
function(){
setTimeout(function(){
swal("AJAXリクエスト完了!");
}, 2000);
});
<link rel="stylesheet" type="text/css"href="dist/sweetalert.css">
<link rel="stylesheet" type="text/css"href="themes/twitter.css">
インストール手順
- 必要なファイルを読み込む
<script src="dist/sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="dist/sweetalert.css">
- ページ読み込み後の初期化処理
swal({
title: "エラー発生!",
text: "エラーメッセージを表示中",
type: "error",
confirmButtonText: "了解"
});
設定パラメータ
| パラメータ名 |
デフォルト値 |
説明 |
| title |
null (必須) |
モーダルタイトル |
| text |
null |
補足説明テキスト |
| type |
null |
メッセージタイプ(warning/error/success/info/input) |
| allowEscapeKey |
true |
エスケープキーによるクローズ許可 |
| customClass |
null |
カスタムCSSクラス指定 |
| allowOutsideClick |
false |
外部クリックによるクローズ許可 |
| showCancelButton |
false |
キャンセルボタン表示フラグ |
| showConfirmButton |
true |
確認ボタン非表示フラグ |
| confirmButtonText |
"OK" |
確認ボタンラベル |
| confirmButtonColor |
"#AEDEF4" |
確認ボタン背景色 |
| cancelButtonText |
"Cancel" |
キャンセルボタンラベル |
| closeOnConfirm |
true |
確認後のクローズフラグ |
| closeOnCancel |
true |
キャンセル後のクローズフラグ |
| imageUrl |
null |
カスタムアイコンURL |
| imageSize |
"80x80" |
アイコンサイズ指定 |
| timer |
null |
自動クローズタイマ(ms) |
| html |
false |
HTML未エスケープフラグ |
| animation |
true |
アニメーション有効フラグ |
| inputType |
"text" |
入力フォームタイプ |
| inputPlaceholder |
null |
入力フォームプレースホルダ |
| inputValue |
null |
初期入力値指定 |
| showLoaderOnConfirm |
false |
確認中のローディング表示 |
メソッド一覧
| メソッド名 |
使用例 |
説明 |
| setDefaults |
swal.setDefaults({ confirmButtonColor: '#0000' }); |
デフォルト設定変更 |
| close |
swal.close(); |
モーダル強制クローズ |
| showInputError |
swal.showInputError("無効なメールアドレス!"); |
入力エラーメッセージ表示 |
| enableButtons/disableButtons |
swal.disableButtons(); |
ボタン操作の有効/無効化 |
実装例
<script>
$(document).ready(function () {
// 削除処理
$(document).on('click', '[id=ApiManagement_del]', function () {
var resourceId = $(this).attr('rel');
swal({
title: "この操作を実行しますか?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "実行",
closeOnConfirm: false
}, function () {
$.ajax({
url: "{:U('App_admin/ApiManagement/del')}",
data: {
api_id: resourceId
},
type: 'post',
cache: false,
success: function (response) {
if (response.code === 1) {
swal("削除完了!", "", "success");
$('.confirm').on('click', function () {
location.reload();
});
} else {
swal("削除失敗!", "", "error");
$('.confirm').on('click', function () {
location.reload();
});
}
}
});
});
});
});
</script>