Variable's value is changed to default everytime using th:with

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

Variable's value is changed to default everytime using th:with

Hiral Vora
Hi,

I want to change the value of parent variable based on the conditions. Below is the code snippet for the same:

<div th:with="parentCategory=${''}">
   <ul th:each="data : ${reportquestionAnswer}">                   
          <li th:if="${parentCategory ne data.name}">
              <div th:with="parentCategory=${data.name}">
                   
              </div>
          </li>
   </ul>
</div>

Here, I am iterating a list using th:each. Initially, the parentCategory variable is empty and if condition evaluates to true and the parentCategory value is changed to name. But for the next iteration, instead of keeping the value of parentCategory unchanged, it is being changed to initial value that is empty/' ' and hence if condition evaluates to true each time.
Is this due to the scope of the variable? Does anyone has any idea about this?

Thanks,
Hiral
Reply | Threaded
Open this post in threaded view
|

Re: Variable's value is changed to default everytime using th:with

Metroids
th:with isn't designed to be used that way.  It just defines a local variable (accessible to only child tags).  You can't use it to keep state that way.