使用 JSP 中的 Foreach 循环拆分 table 中的 Arraylist
Splitting an Arraylist in a table using a Foreach Loop in JSP
关键是,我在 jsp 页面的 foreach 循环中有一个 arrayList,如下所示。所以,我想要有 3 列,每列有 3 行,每行都有连续的值。我应该如何以最简单的方式应用这个场景?
澄清一下:
Column1 Column2 Column3
Data1 Data4 Data7
Data2 Data5 Data8
Data3 Data6 Data9
<table>
<c:set var="numCols" value="3"/>
<c:forEach items="${dataList}" var="info" varStatus = "status">
<c:if test="${status.index % numCols == 0}">
<tr>
</c:if>
<td><input type ="submit" class="data btnColor"
value="${info.dataName}" label ="${info.dataId}" />
</td>
<c:if test="${status.count % numCols == 0 or status.last}">
</tr>
</c:if>
</c:forEach>
您快完成了,但我不确定您对代码示例中的输入做了什么。要使 table 仅显示 3 列,您可以使用 varStatus 标记循环运算符。该文档说明了与之关联的方法。
<c:set var="numCols" value="3"/>
<c:set var="numRows" value="3"/>
<c:set var="rowCount" value="0"/>
<table>
<tr>
// Loop through each element in the dataList and assign it to a variable named info
<c:forEach items="${dataList}" var="info" varStatus="status">
// If the current row count is less than the number of row allowed, proceed
<c:if test="${rowCount lt numRows}">
// Output the element (info) into a table cell
<td><input type ="submit" class="data btnColor" value="${info.dataName}" label ="${info.dataId}" /></td>
// Check to see if the cell that was just created is the 3 one in the row,
// excluding the very first cell of the table (because 0 % 0 is undefined)
<c:if test="${status.count ne 0 && status.count % numCols == 0}">
// Increment the row count
<c:set var="rowCount" value="${rowCount + 1}"/>
// End that row and start a new one
</tr><tr>
</c:if>
</c:if>
</c:forEach>
</tr>
</table>
查看 these examples 以了解有关 c:foreach 标签的更多信息。
关键是,我在 jsp 页面的 foreach 循环中有一个 arrayList,如下所示。所以,我想要有 3 列,每列有 3 行,每行都有连续的值。我应该如何以最简单的方式应用这个场景?
澄清一下:
Column1 Column2 Column3
Data1 Data4 Data7
Data2 Data5 Data8
Data3 Data6 Data9
<table>
<c:set var="numCols" value="3"/>
<c:forEach items="${dataList}" var="info" varStatus = "status">
<c:if test="${status.index % numCols == 0}">
<tr>
</c:if>
<td><input type ="submit" class="data btnColor"
value="${info.dataName}" label ="${info.dataId}" />
</td>
<c:if test="${status.count % numCols == 0 or status.last}">
</tr>
</c:if>
</c:forEach>
您快完成了,但我不确定您对代码示例中的输入做了什么。要使 table 仅显示 3 列,您可以使用 varStatus 标记循环运算符。该文档说明了与之关联的方法。
<c:set var="numCols" value="3"/>
<c:set var="numRows" value="3"/>
<c:set var="rowCount" value="0"/>
<table>
<tr>
// Loop through each element in the dataList and assign it to a variable named info
<c:forEach items="${dataList}" var="info" varStatus="status">
// If the current row count is less than the number of row allowed, proceed
<c:if test="${rowCount lt numRows}">
// Output the element (info) into a table cell
<td><input type ="submit" class="data btnColor" value="${info.dataName}" label ="${info.dataId}" /></td>
// Check to see if the cell that was just created is the 3 one in the row,
// excluding the very first cell of the table (because 0 % 0 is undefined)
<c:if test="${status.count ne 0 && status.count % numCols == 0}">
// Increment the row count
<c:set var="rowCount" value="${rowCount + 1}"/>
// End that row and start a new one
</tr><tr>
</c:if>
</c:if>
</c:forEach>
</tr>
</table>
查看 these examples 以了解有关 c:foreach 标签的更多信息。