如何使用sweetalert和springboot、modelandview删除mysql数据库中的记录

nbewdwxp  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(216)

大家好!如果你能帮我解决这个我无法解决的问题,我将不胜感激。我想用sweetalert删除mysql数据库中的一条记录。就像这张照片:https://imgur.com/bstkuvx
所发生的事情是,当我在“确定”的sweetalert框中单击时,不会删除记录。欢迎所有回复。这个项目是在springboot中使用controller(modelandview)、html和javascript(sweetalert)。非常感谢任何能帮助我的人。维托。mip@gmail.com
控制器.java

@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(sweetalert)

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题