Defining local variable once and incrementing value on each call

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Defining local variable once and incrementing value on each call

Parminder
So I have defined a local variable for a fragment with th:with="index=0". Inside the fragment this variable is used to render field attributes and names. Inside the fragment there is looping and nesting calls to the same fragment. If it was just a simple loop, I could just increment the index value by 1 and use it by th:with="index=${index +1}" but that makes the index variable local to the nested fragment and the incremented value cannot be used by the other objects in the loop.

Basically I want to know if there is a way to increment and refer to the "index" variable without defining it every single time inside a loop.

<span th:include="path/fragments :: fragment1" th:with="object=${someObject},index=0" />

<span th:fragment="fragment1">
            <span th:text="${index}"></span>
            <span th:each="child, rowStat : ${object.child}">
                    <span th:include="path/fragments :: fragment1" th:with="object=${child}, index=${index+rowStat.index+1}" />
            </span>
</span>

This prints out values like
0
1
2 -> 3,4
3
4

and I want it to be like
0
1
2 -> 3,4
5
6
 
   
Reply | Threaded
Open this post in threaded view
|

Re: Defining local variable once and incrementing value on each call

Parminder
I ended up using ${#ids.seq('')} to generate the next count value incase someone needs it.