循环增量计数器 [英] Increment counter with loop

查看:199
本文介绍了循环增量计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我之前的问题有关:

This question is related to my previous question :

Jsp迭代对象列表

我想在for循环中插入从0开始的计数器,我尝试了几种组合远:

I want to insert counter that starts from 0 in my for loop, I've tried several combinations so far :

<c:forEach var="tableEntity" items='${requestScope.tables}'>
   <c:forEach var="rowEntity" items='${tableEntity.rows}' varStatus="count">            
        <c:out value="${count}" />
    </c:forEach>
</c:forEach>



2.



2.

<c:set var="count" value="0" scope="page" />
<c:forEach var="tableEntity" items='${requestScope.tables}'>
   <c:forEach var="rowEntity" items='${tableEntity.rows}'>      
   <%=count++%>  
<c:out value="${count}" />
    </c:forEach>
</c:forEach>

第一种方法的问题是外循环有3个项目,内循环有7个项目,所以每个外部项目计数从0开始。第二个我得到编译错误。这基本上就是我想要的:

Problem with first approach is that outer loop has 3 items and inner loop has 7 items, so for each outer item the count starts from 0. The second one I get compile error. Here is basically what I want :

counter = 0;
outer for loop 
    inner for loop 
       counter++;
       //cout/echo/print counter value should start from 0
    end inner loop
end outer loop

我只是不完全熟悉语法。谢谢

I'm just not totally familiar with the syntax. thank you

推荐答案

请尝试以下方法:

<c:set var="count" value="0" scope="page" />

//in your loops
<c:set var="count" value="${count + 1}" scope="page"/>

这篇关于循环增量计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆