0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel7でsweetalert2を使ってダイアログを表示する

Last updated at Posted at 2021-02-24

【開発環境】

Windows 10 HOME

Laravel Framework 7.25.0
PHP 7.4.7

【目次】

  項目     
- はじめに
- 使い方
- この辺を参考に

はじめに

Laravel7でsweatalert2を使ってダイアログを表示してみてみた。

使い方

CDNを設定する。
(ファイルをダウンロードする方でもお好みで)

qiita.blade.php
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/10.15.4/sweetalert2.css"  />
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/10.15.4/sweetalert2.js"></script>

HTMLのボタン部分。

qiita.blade.php
<input type="image" class="delete" id="delete" src="{{ asset('img/delete.png')}}" alt="削除する">

Javascriptの処理の部分。

qiita.blade.php

<script>
$('#delete').on('click', function(e) {
    e.preventDefault();
    var form = $('form');
	
	swal.fire({
        title: "本当に削除しますか?"
        ,icon: "warning"
        ,showCancelButton: true
        ,confirmButtonColor: "#DD6B55"
        ,confirmButtonText: "削除します!"
		,position : 'center'
		,closeOnConfirm: false
		,allowEscapeKey: true //Escボタン
		,allowOutsideClick : true //枠外クリック
		,showCloseButton : true   //閉じるボタン

	}).then(function(result) { //←この行の記述を修正した結果改善された

		if (result.value) {

			form.submit();

			Swal.fire({
				position: 'center',
				icon: 'success',
				title: 'Successfully Deleted!',
				showConfirmButton: false,
				timer: 2500
			})
		}
	});
});
</script>

画面を見てみると
無事にダイアログが表示されました。

6.png

この辺を参考に

【使い方】 JS Sweet Alert 2

SweetAlert2のパラメータ(引数)の使い方まとめ | 使えるキー情報の使用頻度順一覧

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?