简体   繁体   中英

getting value of selected checkbox from datatable in jsp

I'm using Java EE.

Here's my JSP:

<form  method="POST" enctype="multipart/form-data"action="<c:url value="submitSelected"/>">
<div class="box-body">
    <table id="example" class="table table-bordered table-striped">
        <thead>
            <tr>
                <th>id</th>
                <th>name</th>
                <th><input type="checkbox" id="checkall" /></th>
            </tr>
        </thead>
        <tbody>
            <c:forEach var="category" items="${categoryList}">
                <tr>
                    <td><c:out value="${category.id}"/></td>
                    <td><c:out value="${category.name}"/></td>
                    <td><input type="checkbox" name="selectedCategory" value="${category.id}" /></td>
                </tr>                           
            </c:forEach>
        </table>
    </div>
    <div class="box-footer" id="hidden-div">
        <button type="submit" id="select" >submit</button>
    </div></form>

Here's my JS:

<script type="text/javascript">
$(document).ready(function () {
    $("#example").dataTable({
        "scrollX": true
    });
    $("#example #checkall").click(function () {
        if ($("#example #checkall").is(':checked')) {
            $("#example input[type=checkbox]").each(function () {
                $(this).prop("checked", true);
            });

        } else {
            $("#example input[type=checkbox]").each(function () {
                $(this).prop("checked", false);
            });
        }
    });
});</script>

I get my checkbox values in my servlet using:

String checkboxValues[] = request.getParameterValues("selectedCategory");

I'm using datatable in my JSP. My problem is if my JSP displays more than 1 page and I select some rows from those pages, my servlet only get values only from displayed page. How do I get all the selected values from the servlet? And if I use the checkall checkbox, how do I get all values from the servlet?

JSP is quite a straightforward technology. Any server-side paging which is done in JSP is usually managed by the programmer. JSP doesn't assist you with pagination.

Usually programmers do elaborate orchestration to make pagination work (like passing around parameters like currentPage , totalPages , rowsPerPage etc. and then filtering few rows that will be displayed on the page. They also give hyperlinks for page numbers and more hyperlinks for Previous page and Next page links).

From the code snippet that you have shared, it looks like all rows from categoryList are displayed. If categoryList has 1000 rows, all 1000 rows will be displayed in the same page.

For pagination to work, there should be some servlet or struts Action where the categoryList is managed. Only few rows which are relevant to the current page number should be loaded into that array list.

Considering all this, you can understand that even stuff like 'check-all' would have to be managed by the developer using a fair bit of coding.

Whenever the user clicks to navigate away from a page, you may have to append the category ids corresponding to the selected check-boxes to the URL for the next page, and then on the server maintain an array list of selected category ids. You would also have to clear this list if the user performs a new search and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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