以下是我的jsp代码。
<c:forEach items="${nameList}" var="studentList">
<tr>
<td style="padding-bottom: .5em;">
<a id="student" href="number?regNo=<c:out value="${studentList.regNo}"/>">
<span class="eachStudent">
<span class="studentName"><c:out value="${studentList.name}"/></span><br/>
<span class="collName"><c:out value="${studentList.collName}"/></span><br/>
<span class="deptName"><c:out value="${studentList.deptName}"/></span><br/>
</span>
</a>
</td>
</tr>
</c:forEach>
上面的c:forEach
列表
Name CollName DeptName
ABCD coll1 dept1
kfkdb coll1 dept2
jbdd coll2 dept3
以下代码分别列出了 collName
和 deptName
。
<div>
Filter students by College (not required):
<select id="byCollege" name="byCollege" >
<c:forEach items="${uniqueCollList}" var="uniqueCollList">
<option value="${uniqueCollList}"/>
${uniqueCollList}</option >
</c:forEach >
</select >
</div>
<div>
Filter students by Department Name (not required):
<select id="byDept" name="byDept" >
<c:forEach items="${uniqueDeptList}" var="uniqueDeptList">
<option value="${uniqueDeptList}"/>
${uniqueDeptList}</option >
</c:forEach >
</select >
</div>
现在,当我从第一个下拉列表中选择一个值时,我想使用下拉列表中选择的值来过滤 foreach
给出的结果。我想在前端本身执行此操作,而不是在后端执行此操作。我可以知道我该怎么做吗?
使用第一个下拉列表的值过滤 c:foreach
结果后,如果我在第二个下拉列表中选择一个值,我希望 c:foreach
的更新结果为使用第二个下拉列表中选择的值进行过滤。
我该怎么做?
如果我想在后端执行此操作,我该怎么办?
PS:以下是第一次发送列表的 Controller 代码
@RequestMapping(value = "/name", method = RequestMethod.POST, params = { "studentName" })
public String searchStudentByCollOrDept(@RequestParam(value = "studentName", required = true)String studentName, ModelMap model){
List<OneStudentResult> nameList = resultService.getStudentList(studentName);
//TODO, null value check.
if(nameList.size() == 0 || nameList == null){
return "header";
}
if(nameList.size() != 0){
// Iterate the list that we get and add only one time a collName and deptname.So a Treeset.
Set<String> uniqueCollList = new TreeSet<String>();
Iterator<OneStudentResult> itr = nameList.iterator();
while(itr.hasNext()){
String collName = itr.next().getCollName();
if(!uniqueCollList.contains(collName)){
uniqueCollList.add(collName);
}
}
uniqueCollList.add(" Select a College ");
model.addAttribute("uniqueCollList", uniqueCollList);
Set<String> uniqueDeptList = new TreeSet<String>();
Iterator<OneStudentResult> itrDeptList = nameList.iterator();
while(itrDeptList.hasNext()){
String deptName = itrDeptList.next().getDeptName();
if(!uniqueDeptList.contains(deptName)){
uniqueDeptList.add(deptName);
}
}
uniqueDeptList.add(" Select a Department ");
model.addAttribute("uniqueDeptList", uniqueDeptList);
}
model.addAttribute("nameList", nameList);
return "nameResult";
}
最佳答案
恐怕没有简单的方法可以解决您的问题。您应该考虑使用 ajax 更新在服务器端执行此操作。客户端过滤要么要求您解析由 StudentList 生成的 HTML 中的数据,要么要求您将数据作为 JSON 格式的数组注入(inject)。在这两种情况下,您最终都会得到双倍的数据(服务器和客户端)。将数据放在客户端只会允许您禁用某些选项值,而不能隐藏它们。因此,如果您想要真正的过滤而不显示某些选项,那么您应该在服务器上过滤列表。为此,您应该将第一个下拉列表“byCollege”中的选定选项发布到后端,以便检索过滤后的“uniqueDeptList”,用于重建“byDept”复选框。您可能想使用 jQuery 来完成这两项任务。
步骤:
<强>1。处理“byCollege”下拉列表的更改事件
<强>2。将所选选项值发布到您的 servlet
<强>3。过滤 servlet 中的数据并将过滤后的数据作为 POST 响应返回(本例中省略)
<强>4。删除旧的选择选项并从过滤后的数据中重新创建它们
$("#byCollege").change(function() {
$("select option:selected").first().each(function() {
// Get and convert the data for sending
// Example: This variable contains the selected option-text
var outdata = $(this).text();
// Send the data as an ajax POST request
$.ajax({
url: "yourjsonservlet",
type: 'POST',
dataType: 'json',
data: JSON.stringify(outdata),
contentType: 'application/json',
mimeType: 'application/json',
success: function(data) {
// Remove old select options from the DOM
$('#byCollege')
.find('option')
.remove()
.end();
// Parse data and append new select options
//(omitted here; e.g. $('#byCollege').append($("<option>").attr(...))
},
error: function(data, status, er) {
alert("error: " + data + " status: " + status + " er:" + er);
}
});
});
});
关于java - 如何过滤 c :forEach using the value selected in a drop down? 给出的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26352959/