从 jsp 中的数据表中获取所选复选框的值
getting value of selected checkbox from datatable in jsp
我正在使用 Java EE。
这是我的 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>
这是我的 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>
我使用以下方法在我的 servlet 中获取我的复选框值:
String checkboxValues[] = request.getParameterValues("selectedCategory");
我在 JSP 中使用数据表。我的问题是,如果我的 JSP 显示多于 1 页并且我 select 来自这些页面的一些行,我的 servlet 仅从显示的页面获取值。如何从 servlet 获取所有 selected 值?如果我使用 checkall 复选框,我如何从 servlet 获取所有值?
JSP 是一项非常简单的技术。在 JSP 中完成的任何服务器端分页通常由程序员管理。 JSP 不帮助您进行分页。
通常程序员会精心编排以使分页工作(例如传递 currentPage
、totalPages
、rowsPerPage
等参数,然后过滤将显示在页。他们还提供了页码的超链接以及 上一页 和 下一页 链接的更多超链接)。
从您共享的代码片段来看,似乎显示了 categoryList
中的所有行。如果 categoryList
有 1000 行,则所有 1000 行将显示在同一页中。
要使分页工作,应该有一些 servlet 或 struts Action
来管理 categoryList
。只有与当前页码相关的几行应该加载到该数组列表中。
考虑到所有这些,您可以理解即使像 'check-all' 这样的东西也必须由开发人员使用相当多的编码来管理。
每当用户单击离开页面时,您可能必须将与所选复选框对应的类别 ID 附加到下一页的 URL,然后在服务器上维护一个所选类别 ID 的数组列表。如果用户执行新的搜索等等,您还必须清除此列表。
我正在使用 Java EE。
这是我的 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>
这是我的 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>
我使用以下方法在我的 servlet 中获取我的复选框值:
String checkboxValues[] = request.getParameterValues("selectedCategory");
我在 JSP 中使用数据表。我的问题是,如果我的 JSP 显示多于 1 页并且我 select 来自这些页面的一些行,我的 servlet 仅从显示的页面获取值。如何从 servlet 获取所有 selected 值?如果我使用 checkall 复选框,我如何从 servlet 获取所有值?
JSP 是一项非常简单的技术。在 JSP 中完成的任何服务器端分页通常由程序员管理。 JSP 不帮助您进行分页。
通常程序员会精心编排以使分页工作(例如传递 currentPage
、totalPages
、rowsPerPage
等参数,然后过滤将显示在页。他们还提供了页码的超链接以及 上一页 和 下一页 链接的更多超链接)。
从您共享的代码片段来看,似乎显示了 categoryList
中的所有行。如果 categoryList
有 1000 行,则所有 1000 行将显示在同一页中。
要使分页工作,应该有一些 servlet 或 struts Action
来管理 categoryList
。只有与当前页码相关的几行应该加载到该数组列表中。
考虑到所有这些,您可以理解即使像 'check-all' 这样的东西也必须由开发人员使用相当多的编码来管理。
每当用户单击离开页面时,您可能必须将与所选复选框对应的类别 ID 附加到下一页的 URL,然后在服务器上维护一个所选类别 ID 的数组列表。如果用户执行新的搜索等等,您还必须清除此列表。