[英]No experience with JS- how to use sweet alert to confirm a form submission?
[英]Symfony - How to use route for form submission using Sweet Alert?
編輯:如何確認以“甜蜜警報”的形式提交所選用戶ID? 請看,我有此表,該表顯示我的網頁中的所有注冊用戶。 我的問題是我不知道如何在我的樹枝模板中生成Symfony到<button>
標記的路由。 我不能使用<a>
標記,因為僅SWAL執行時data-type
是使用和它的只適用於<button>
標記,它不能使用href
屬性等href="{{ path('path_to_route') }}"
。 我在調用{{ user.id }}
也遇到問題,因為它沒有獲取我選擇的按鈕的用戶ID。 我有來自各種來源的這段代碼,但仍然有問題。 急需幫助!
更新:我無法下載FOSJsRoutingBundle,因為我們在一家有安裝限制的公司里工作,不幸的是,安裝捆綁包是其中的一部分。 有沒有其他選擇?
我的樹枝:
<div class="row clearfix js-sweetalert">
<div class="btn-group" role="group">
<button class="btn btn-warning waves-effect" data-type="cancel"><i class="material-icons">build</i></button>
<button class="btn btn-danger waves-effect" data-type="delete-user" data-userId="{{ user.id }}"><i class="material-icons">delete</i></button>
</div>
</div>
這是我的控制器:
public function deleteUserAction($id)
{
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->findUserBy(['id' => $id]);
if ($user == null) {
throw new NotFoundHttpException('User not found for user' . $user);
}
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
$userManager->deleteUser($user);
$this->setFlash('user_deleted', $user . ' has been successfully deleted!');
$url = $this->generateUrl('fos_user_userList');
return new RedirectResponse($url);
}
這是我的Sweet Alert腳本:
$(function () {
$('.js-sweetalert button').on('click', function () {
var type = $(this).data('type');
if (type === 'basic') {
showBasicMessage();
}
else if (type === 'delete-user') {
var userId = $(this).data('userId');
showDeleteUserMessage();
}
function showDeleteUserMessage() {
swal({
title: "Are you sure you want to delete this user?",
text: "You will not be able to recover this user",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
}, function (isConfirm) {
if (isConfirm) {
var data = {id : userId};
$.ajax({
type: "POST",
url: "{{ path('fos_user_deleteUser') }}",
data: data,
success: function(data, dataType) {
swal("Deleted", "The user has been removed from the Database", "Success");
},
error: function (xhr, ajaxOptions, thrownError) {
swal("Error deleting!", "Please try again", "error");
}
});
} else {}
});
}
您可以使用FosJsRoutingBundle在Javascript代碼中生成路由,或將js代碼移到樹枝模板中以使用路徑樹枝函數
在html中添加用戶ID
<button class="btn btn-danger waves-effect" data-userId="1" data-type="delete-user"><i class="material-icons">delete forever</i></button>
在單擊按鈕時,您需要獲取用戶ID值並將其傳遞給函數
var type = $(this).data('type');
if (type === 'delete-user') {
var userId = $(this).data('userId');
showDeleteUserMessage(userId);
}
在函數(isConfirm)中,您可以調用ajax
if (isConfirm) {
//ajax request here
var data = {id : userId}; //u need to post id
$.ajax({
type: "POST",
url: "{{ path('fos_user_deleteUser') }}",
data: data,
success: function(data, dataType)
{
alert(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert('Error : ' + errorThrown);
}
} else {
swal("Cancelled", "not deleting", "error");
}
請檢查您的js文件打開大括號
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.