How to delete a record in the Mysql database using Sweetalert in Spring Boot Modelndview

Asked

Viewed 65 times

0

I would be grateful if you could help me solve this problem that I am not managing to do. I wanted to delete a record in the Mysql Database using Sweetalert. inserir a descrição da imagem aqui

What’s happening is that it doesn’t delete my registration as soon as I click on the "OK" in the Sweetalert box. All answers will be welcomed. The project is in Spring Boot with Controller (Modelandview), HTML and Javascript (Sweetalert). Thank you very much to anyone who can help me. [email protected]

Java controller.

@RequestMapping("delete.htm")
public ModelAndView Delete(HttpServletRequest request) {
    id = Integer.parseInt(request.getParameter("id"));
    String sql = "delete from escola where id=" + id;
    this.jdbcTemplate.update(sql);
    return new ModelAndView("redirect:/escolas.htm");
}

Html

 <c:forEach var="bd" items="${lista}">
                                <tr class="text-center">
                                    <td style="text-align:left; font-size:10px">${bd.escola}</td>                                      
                                    <td style="text-align:left; font-size:10px">${bd.localidade}</td>
                                    <td style="text-align:left; font-size:10px">${bd.email}</td>
                                    <td style="text-align:left; font-size:10px">${bd.telefone}</td>
                                    <td style="text-align:left; font-size:10px">${bd.ano_lectivo}</td>
                                    <td style="text-align:left; font-size:10px">${bd.obs}</td>
                                    <td style="text-align:left; font-size:10px">${bd.registo}</td>
                                    
                                    <td class="d-flex">
                                    <a style="font-size:10px" href="editar.htm?id=${bd.id}" class="btn btn-warning">Editar</a>
                                    
                                    <input class="deleteid" type="hidden" value="${bd.id}">
                                    <a class="elimina_registo btn btn-danger" style="font-size:12px;">Eliminar</a>
                                    </td>
                                </tr>
                            </c:forEach>

Javascript

 <script>
                            $(document).ready(function () {

                                $('.elimina_registo').on('click', function (e) {
                                    e.preventDefault();
                                    var deleteid = $(this).closest("tr").find('.deleteid').val();
                                    
                                    swal({
                                        title: "Tem a certeza que deseja eliminar este registo?",
                                        text: "ATENÇÃO: Uma vez eliminado, não poderá voltar a recuperar a informação!",
                                        icon: "warning",
                                        buttons: true,
                                        dangerMode: true,
                                    })
                                            .then((willDelete) => {
                                                if (willDelete) {
                                                    eliminar(deleteid);
                                                    swal("Registo eliminado com sucesso!", {
                                                        icon: "success",
                                                    }).then((willDelete) => {
                                                        if (willDelete) {
                                                            parent.location.href = "escolas.htm";
                                                        }
                                                    });
                                                } else {

                                                    swal({
                                                        title: "Parabéns!",
                                                        text: "O registo da Escola NÃO foi eliminado!!",
                                                        icon: "success",
                                                        button: "Ok!",
                                                    });
                                                }
                                            });
                                });
                                function eliminar(deleteid) {
                                    var url = "delete.htm?"
                                    $.ajax({
                                        url: url,
                                        data: "id=" + deleteid,
                                        success: function (data, textStatus, jqXHR) {
                                        }
                                    });
                                }

                            });
    </script> 
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.