Increment counter with loop

前端 未结 4 417
梦毁少年i
梦毁少年i 2021-01-30 08:21

This question is related to my previous question :

Jsp iterate trough object list

I want to insert counter that starts from 0 in my for loop, I\'ve tried several

相关标签:
4条回答
  • 2021-01-30 08:43

    The varStatus references to LoopTagStatus which has a getIndex() method.

    So:

    <c:forEach var="tableEntity" items='${requestScope.tables}' varStatus="outer">
       <c:forEach var="rowEntity" items='${tableEntity.rows}' varStatus="inner">            
            <c:out value="${(outer.index * fn:length(tableEntity.rows)) + inner.index}" />
        </c:forEach>
    </c:forEach>
    

    See also:

    • Hidden features of JSP/Servlet
    0 讨论(0)
  • 2021-01-30 08:56

    Try the following:

    <c:set var="count" value="0" scope="page" />
    
    //in your loops
    <c:set var="count" value="${count + 1}" scope="page"/>
    
    0 讨论(0)
  • 2021-01-30 09:02

    You can use varStatus in your c:forEach loop

    In your first example you can get the counter to work properly as follows...

    <c:forEach var="tableEntity" items='${requestScope.tables}'>
       <c:forEach var="rowEntity" items='${tableEntity.rows}' varStatus="count">            
            my count is ${count.count}
        </c:forEach>
    </c:forEach>
    
    0 讨论(0)
  • 2021-01-30 09:05

    what led me to this page is that I set within a page then the inside of an included page I did the increment

    and here is the problem

    so to solve such a problem, simply use scope="request" when you declare the variable or the increment

    //when you set the variale add scope="request"
    <c:set var="nFilters" value="${0}" scope="request"/>
    //the increment, it can be happened inside an included page
    <c:set var="nFilters" value="${nFilters + 1}"  scope="request" />
    

    hope this saves your time

    0 讨论(0)
提交回复
热议问题