How to use a var counter in Thymeleaf?

0

Is it possible to create a counter variable in thymeleaf? I need an incremental variable to pass as tag IDs in HTML. I have the following loopbacks, where each loop will be iterated x different times, so I can not use the index of any of the loops to set the id of the tags that should be sequential (value0, value1, value2 ...) p>

<div class="col m12">
   <div th:each="e, index : ${produto.etapasTrabalho}">
      <div th:each="t : ${e.tarefas}">
         <div th:each="i : ${t.itensTarefa}">
            <div id="campo" class="campo input-field col m8" th:if="${i.tipo.toString() == 'Data'}">
               <input type="text" id="id" th:value="${i.id}" th:hidden="hidden" />
               <label th:text="${i.nome}"></label>
               <input type="text" th:value="${i.valor}" class="datepicker" th:id="'valor'+${index}" />
            </div>
            <div id="campo" class="campo input-field col m8" th:if="${i.tipo.toString() == 'Texto'}">
               <input type="text" id="id" th:value="${i.id}" th:hidden="hidden" />
               <input type="text" th:id="'valor'+${index}" th:value="${i.valor}" />
               <label th:text="${i.nome}"></label>
            </div>
            <div id="campo" class="campo input-field col m8" th:if="${i.tipo.toString() == 'Texto Longo'}">
               <input type="text" id="id" th:value="${i.id}" th:hidden="hidden" />
               <textarea class="materialize-textarea" th:value="${i.valor}" th:id="'valor'+${index}"></textarea>
               <label th:text="${i.nome}"></label>
            </div>
            <div id="campo" class="campo input-field col m8" th:if="${i.tipo.toString() == 'Dinheiro $'}">
               <input type="text" id="id" th:value="${i.id}" th:hidden="hidden" />
               <input class="money" type="text" th:value="${i.valor}" th:id="'valor'+${index}" />
               <label th:text="${i.nome}"></label>
            </div>
            <div id="campo" class="campo" th:if="${i.tipo.toString() == 'Check'}">
               <input type="text" id="id" th:value="${i.id}" th:hidden="hidden" />
               <input type="checkbox" class="filled-in" id="check" th:id="'valor'+${index}" />
               <label for="check" th:text="${i.nome}"></label>
            </div>
         </div>
      </div>
      <br/>
   </div>
</div>

If it is not possible, can I use a javascript variable?

    
asked by anonymous 24.04.2018 / 19:29

1 answer

0

I found the solution through javascript, I simply search all fields contain a certain class and I define the id attribute of the element with the index of each.

$('.campo').each(function(i) {
       $(this).attr('id', 'valor' + i);
    });
    
25.04.2018 / 15:59