Nb12se
Nb12se

Reputation: 513

Spring MVC delete using modal

I have a problem on delete item using modal. I can delete the using just the href obtaining the id and passing it to the controller. But i dont know how to get the id of the item in modal and delete it.

List of persons and actions to be performed

<c:forEach var="user" items="${listpersons}">
                <tr>
                    <td><a href="#">${user.username}</a><!--  <span>Clients</span> --> </td>
                    <td><a href="#">${user.email}</a></td>
                    <td>
                        <button class="btn btn-white btn-sm" data-toggle="tooltip" data-placement="top" title="Compose"><a href="#"><i class="fa fa-envelope-o"></i></a></button>
                        <a href="${pageContext.request.contextPath}/edit?id=${user.id}&name=${user.username}&password=${user.password}&email=${user.email}"><i class="fa fa-edit" title="Edit"></i></a></button>
                        <a href="${pageContext.request.contextPath}/delete?id=${user.id}" title="Delete"><i class="fa fa-trash-o"></i></a>
                        <a href="#myModal" role="button" class="btn btn-large btn-primary" data-toggle="modal">Launch Demo Modal</a>
                    </td>
                    <td class="text-right mail-date">Jan 16</td>
                </tr>
 </c:forEach>

this part is the modal button when clicked pops.up a if i want to really delete the item

<a href="#myModal" role="button" class="btn btn-large btn-primary" data-toggle="modal">Launch Demo Modal</a>

this is my delete button. i can delete here but i want to delete using modal

<a href="${pageContext.request.contextPath}/delete?id=${user.id}" title="Delete"><i class="fa fa-trash-o"></i></a>

and this is my modal popup

<div id="myModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Confirm Delete</h4>
            </div>

            <div class="modal-body">
                <p>Are you sure you want to delete this user? </p>
            </div>
            <div class="modal-footer">

                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <a href="${pageContext.request.contextPath}/delete?id=${user.id}" title="Delete"><i class="fa fa-trash-o"></i></a>
            </div>
        </div>
    </div>
</div>

I dont know how to pass the id of the person in the modal popup. delete doesnt work. it doent recognize any id

this is the controller

@RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ModelAndView delete(@ModelAttribute("SpringWeb")User user, ModelMap model, HttpServletRequest request)
    {
        try
        {
            UserDao ud = new UserJDBC();
            int id = Integer.parseInt(request.getParameter("id"));
            int delete = ud.deleteUser(id);
            model.addAttribute("message", "User deleted Successfuly");

        }
        catch(Exception e)
        {
            System.out.print(e);
            model.addAttribute("message", "Error occured in deleting user.");
        }

        return new ModelAndView("admin-view-users");
    }

User is not deleted. i get this delete?id=0 when clicking the Delete on the modal

Upvotes: 2

Views: 6932

Answers (1)

Harshal Patil
Harshal Patil

Reputation: 6759

Append ${user.id} with href value of popup show button i.e.

#myModal_${user.id}

Add modal popup inside the foreach loop and now append ${user.id} with popup id. i.e. myModal_${user.id}

replace your foreach with this code and remove popup.

<c:forEach var="user" items="${listpersons}">
<tr>
    <td><a href="#">${user.username}</a><!--  <span>Clients</span> --> </td>
    <td><a href="#">${user.email}</a></td>
    <td>
        <button class="btn btn-white btn-sm" data-toggle="tooltip" data-placement="top" title="Compose"><a href="#"><i class="fa fa-envelope-o"></i></a></button>
        <a href="${pageContext.request.contextPath}/edit?id=${user.id}&name=${user.username}&password=${user.password}&email=${user.email}"><i class="fa fa-edit" title="Edit"></i></a></button>
        <a href="${pageContext.request.contextPath}/delete?id=${user.id}" title="Delete"><i class="fa fa-trash-o"></i></a>
        <a href="#myModal_${user.id}" role="button" class="btn btn-large btn-primary" data-toggle="modal">Launch Demo Modal</a>
    </td>
    <td class="text-right mail-date">Jan 16</td>
</tr>

<div id="myModal_${user.id}" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Confirm Delete</h4>
            </div>

            <div class="modal-body">
                <p>Are you sure you want to delete this user? </p>
            </div>
            <div class="modal-footer">

                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <a href="${pageContext.request.contextPath}/delete?id=${user.id}" title="Delete"><i class="fa fa-trash-o"></i>Delete</a>
            </div>
        </div>
    </div>
</div>  
</c:forEach>

Note:-Please do not forgot to add modal popup inside foreach.

Upvotes: 3

Related Questions