如何将一个ArrayList分解并显示为多个表列 [英] How to break and display one ArrayList into multiple table columns
问题描述
我有一个servlet,它加载一个属性文件,并在ArrayList对象中包含100个测试用例名称的列表。将servlet加载到JSP后,该JSP在表中显示列表。列表很长,所以我想要一些优雅的方式在表中显示它,以便它分成例如JSP上的三列或四列。
I have a servlet which loads a properties file and contains a list of 100 test case names into an ArrayList object. After loading the servlet forwards to a JSP which displays the list in a table. The list is long so I would like some elegant way to display it in a table so that it breaks up into, for example, three or four columns on the JSP.
我现在所做的是将列表分成servlet中的三个子列表:
What I do now is break up the list into three sublists in the servlet:
//load properties
Properties props = new Properties();
ArrayList<String> tests = new ArrayList<String>();
props.load(getServletContext().getResourceAsStream("/WEB-INF/sailcertifier.properties"));
Pattern pattern = Pattern.compile("[A-Z]{3}-[0-9]{2}");
for (Enumeration<Object> e = props.keys(); e.hasMoreElements();) {
String key = (String) e.nextElement();
Matcher m = pattern.matcher(key);
if (m.find())
tests.add(key);
}
Collections.sort(tests, new TestOrderComparator());
confBean.setPossibleTests(tests.toArray(new String[tests.size()]));
int third = tests.size() / 3;
List<String> testSubset1 = tests.subList(0, third);
List<String> testSubset2 = tests.subList(third, third * 2);
List<String> testSubset3 = tests.subList(third * 2, tests.size());
//store the bean as a request attribute
request.setAttribute("testSet1", testSubset1.toArray(new String[testSubset1.size()]));
request.setAttribute("testSet2", testSubset2.toArray(new String[testSubset2.size()]));
request.setAttribute("testSet3", testSubset3.toArray(new String[testSubset3.size()]));
request.setAttribute("testsConf", confBean);
request.setAttribute("certProps", props);
//forward to tests selection page
String url = "/sailcertifier/jsp/testsSelection.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
response.setContentType("application/javascript");
try {
dispatcher.forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
}
在JSP中,我像这样迭代子列表(添加一些html元素每种情况):
In the JSP I iterate through the sublists like so (adding some html elements for each case):
<table>
<tr>
<td style="width: 33%">
<table>
<tr>
<td>
<c:forEach var="testName" items="${testSet1}">
<tr>
<td><label for="${testName}" id=${testName}Label>${testName}</label></td>
<td><input id=${testName} type="checkbox" value=${testName} name="selTest"></input></td>
<td><input id=${testName}Run type="button" value="Run Test" name="runButtons" /></td>
<td><input id=${testName}ManPass type="button" value="Manual Pass" name="manPassButtons"/></td>
<td><div id=${testName}Status style="width:100px"></td>
<td></td>
<td></td>
</tr>
</c:forEach>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
</td>
</tr>
</table>
</td>
<td style="width: 33%">
<table>
<tr>
<td>
<c:forEach var="testName" items="${testSet2}">
<tr>
<td><label for="${testName}" id=${testName}Label>${testName}</label></td>
<td><input id=${testName} type="checkbox" value=${testName} name="selTest"></input></td>
<td><input id=${testName}Run type="button" value="Run Test" name="runButtons" /></td>
<td><input id=${testName}ManPass type="button" value="Manual Pass" name="manPassButtons"/></td>
<td><div id=${testName}Status style="width:100px"></td>
<td></td>
<td></td>
</tr>
</c:forEach>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
</td>
</tr>
</table>
</td>
<td style="width: 33%">
<table>
<tr>
<td>
<c:forEach var="testName" items="${testSet3}">
<tr>
<td><label for="${testName}" id=${testName}Label>${testName}</label></td>
<td><input id=${testName} type="checkbox" value=${testName} name="selTest"></input></td>
<td><input id=${testName}Run type="button" value="Run Test" name="runButtons" /></td>
<td><input id=${testName}ManPass type="button" value="Manual Pass" name="manPassButtons"/></td>
<td><div id=${testName}Status style="width:100px"></td>
<td></td>
</tr>
</c:forEach>
</td>
</tr>
</table>
</td>
</tr>
</table>
我承认使用三个JSTL for循环有点难看(还没弄明白使用完整原版列表)。有没有更清晰的方法来处理这个,通过使用一些jquery插件(如 jquery grid plugin )或其他图书馆( displaytag ?)为我们平均分配行?
I admit using three JSTL for loops is kind of ugly (haven't figured out use the full original list yet). Is there a cleaner way to handle this, by using some jquery plugin (like jquery grid plugin) or other library (displaytag?) to handle the breaking up into rows evenly for me?
推荐答案
打印< tr>
,然后遍历数组列表,然后打印每个项目在< td>
中打印< / tr>< tr>
每 n items,然后结束数组列表上的循环,最后打印< / tr>
。
Print a <tr>
, then loop over the array list, then print each item in a <td>
and print a </tr><tr>
every n items, then end the loop over the array list and finally print a </tr>
afterwards.
EG每隔3个项目换一个新行:
E.g. a new row every 3rd item:
<table>
<tr>
<c:forEach items="${items}" var="item" varStatus="loop">
<c:if test="${not loop.first and loop.index % 3 == 0}">
</tr><tr>
</c:if>
<td>${item}</td>
</c:forEach>
</tr>
</table>
$ {loop.index}
返回当前迭代轮次的索引。 %3
只返回 0
,如果它可以被3分割而没有余数(因此,当索引为0时) ,3,6,9,12等)。
The ${loop.index}
returns the index of the current iteration round. The % 3
would only return 0
if it is dividable by 3 without a remainder (thus, when the index is 0, 3, 6, 9, 12, etc).
这篇关于如何将一个ArrayList分解并显示为多个表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!