简体   繁体   English

Symfony-如何使用路由使用Sweet Alert提交表单?

[英]Symfony - How to use route for form submission using Sweet Alert?

EDIT: How can I confirm submission of a selected user id in a form of a Sweet Alert? 编辑:如何确认以“甜蜜警报”的形式提交所选用户ID? See, I have this table which displays all the registered users in my webpage. 请看,我有此表,该表显示我的网页中的所有注册用户。 My problem is I don't know how to generate Symfony's route to a <button> tag in my twig template. 我的问题是我不知道如何在我的树枝模板中生成Symfony到<button>标记的路由。 I can't use <a> tag because SWAL only executes when data-type is use and it's only available in <button> tag, which can't use a href attribute like href="{{ path('path_to_route') }}" . 我不能使用<a>标记,因为仅SWAL执行时data-type是使用和它的只适用于<button>标记,它不能使用href属性等href="{{ path('path_to_route') }}" I also had problem calling for the {{ user.id }} because it doesn't get the user id of the button I selected. 我在调用{{ user.id }}也遇到问题,因为它没有获取我选择的按钮的用户ID。 I have this code from various sources but still there's somethings wrong. 我有来自各种来源的这段代码,但仍然有问题。 Badly need help! 急需帮助!

UPDATE: I can't download FOSJsRoutingBundle because we are working in a company with some installation restriction and unfortunately, installing a Bundle is part of it. 更新:我无法下载FOSJsRoutingBundle,因为我们在一家有安装限制的公司里工作,不幸的是,安装捆绑包是其中的一部分。 Is there any alternative? 有没有其他选择?

My Twig: 我的树枝:

<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>

This is my Controller: 这是我的控制器:

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);
    }

This is my Sweet Alert script: 这是我的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代码移到树枝模板中以使用路径树枝函数

In html add the user id 在html中添加用户ID

 <button class="btn btn-danger waves-effect" data-userId="1" data-type="delete-user"><i class="material-icons">delete forever</i></button>

at the time of button click u need to take the user id value and pass it through the function 在单击按钮时,您需要获取用户ID值并将其传递给函数

var type = $(this).data('type');
if (type === 'delete-user') {
  var userId = $(this).data('userId');
    showDeleteUserMessage(userId);
}

Inside the function(isConfirm) u can call ajax 在函数(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");
    }

plz check ur js file open an close braces 请检查您的js文件打开大括号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM