Joe February 2016

How to pass table row data to a servlet from the jsp?

I have a html table in my jasp that presenting pairs of id & month, it looks like this:

enter image description here

the jsp looks like this:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="Post" action="payCheckInfo">
            <table id="ptable" border="1">

                <tr>
                    <td style="text-align: center;">ID</td>
                    <td style="text-align: center;">Month</td>
                </tr>



                <c:forEach var="entry" items="${EmployeeHashMap}" >
                    <!-- entry.key is employee.key -->
                    <!-- entry.value is employee.skills -->
                    <c:forEach var="month" items="${entry.value}" >
                        <tr>
                            <td><a href="payCheckInfo">${entry.key}</a></td>
                            <td>${month}</td>
                        </tr>
                    </c:forEach>
                </c:forEach>

            </table>
        </form>
    </body>
</html>Type a message

Now I want that every time that a user will click on some id (which is a link) I will go to payCheckInfo which is my servlet and from there I can get to the specific row data, cause in payCheckInfo I have a method that is doing what I need to do with the data

Answers


Satya February 2016

Get specific row data of a row.

$("tr.table").click(function() {
    var tableData = $(this).children("td").map(function() {
        return $(this).text();
    }).get();

    alert($.trim(tableData[0]) + " , " + $.trim(tableData[1]));
    //Here, Make a Ajax call to your Servlet payCheckInfo 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
           <tr class="table">
                <td class="table">
                     ID1
                </td>
                <td>
                    Month1
                </td>
            </tr>
            <tr class="table">
                <td class="table">
                     ID2
                </td>
                <td>
                    Month2
                </td>
            </tr>
</table>

Post Status

Asked in February 2016
Viewed 2,672 times
Voted 10
Answered 1 times

Search




Leave an answer