遍历list并显示为多列

来源:互联网 发布:买家如何开通淘宝客 编辑:程序博客网 时间:2024/06/04 19:41

本来想用标签实现,可是,跟标签不是很熟,^_^,所以里面还是有很多jsp脚本,见谅哦!

<table width="321" height="251" align="center" cellpadding="0" cellspacing="0" border="1">
      <% int index=1; %>
  <tr>
      <logic:iterate name="list" id="item">
      <td width="66" align="center"><bean:write name="item" property="id" /></td>
     
       <%if(index%4==0){ %>
   </tr><tr>
       <%} %>
       <% index++; %>
     </logic:iterate>
     <%
  List list=(ArrayList)request.getAttribute("list");
  if(list.size()%4==3){
  %>
      <td width="66" align="center">&nbsp;</td>
    <%} %>
<%
  if(list.size()%4==2){
  %>
      <td width="66" align="center">&nbsp;</td>
      <td width="66" align="center">&nbsp;</td>
    <%} %>
    <%
  if(list.size()%4==1){
  %>
      <td width="66" align="center">&nbsp;</td>
      <td width="66" align="center">&nbsp;</td>
      <td width="66" align="center">&nbsp;</td>
    <%} %>
    </table>

^_^,今天下班了,研究了一下下标签,终于用标签实现了,贴一下代码给大家:

<table width="321" height="251" align="center" cellpadding="0" cellspacing="0" border="1">
      <c:set value="1" var="index"></c:set>
  <tr>
      <logic:iterate name="list" id="item">
      <td width="66" align="center"><bean:write name="item" property="id" /></td>
      <c:if test="${index%4==0}">
   </tr><tr>
       </c:if>
       <c:set value="${index+1}" var="index" />
     </logic:iterate>
     <c:set value="${fn:length(list)%4}" var="size"/>
     <c:if test="${size==3}">
      <td width="66" align="center">&nbsp;</td>
    </c:if>
<c:if test="${size==2}">
      <td width="66" align="center">&nbsp;</td>
      <td width="66" align="center">&nbsp;</td>
    </c:if>
    <c:if test="${size==1}">
      <td width="66" align="center">&nbsp;</td>
      <td width="66" align="center">&nbsp;</td>
      <td width="66" align="center">&nbsp;</td>
    </c:if>

    </table>