Durga
Durga

Reputation: 565

How to iterate simultaneously over two lists using thymeleaf

I am new to thymeleaf and In my current spring-boot project, I have this thymeleaf code:

<table class="table table-hover" id="table">
          <thead  style="background-color:#CCE5FF">
            <tr>  
                <th>ID</th>                     
                <th>Code</th>                   
                <th>Created Date</th>
                <th>EMP Account</th>
                <th>Bank Name</th>
                <th></th> 
            </tr>
            </thead>
            <tbody>
            <tr th:each="emp,iterStat : ${empList}">//accList-another list
                <td th:text="${emp.id}">ID</td>
                <td th:text="${emp.mdrcode}">Code</td>
                <td th:text="${emp.createDate}">Created Date</td> 
                <td th:text="${}">Emp Account</td>
                <td th:text="${}">Bank Name</td>                     
                <td><a  id="editview" class="btn btn-sm btn-default" th:href="@{/}"><i class="fa fa-edit"></i> View</a></td>
            </tr>
            </tbody>
          </table>
            </tbody>
          </table>

Here i have details in two lists,one list values getting successfully. But i don't know how to get second list values.

I am trying to iterate two list at a time in th:each but i didn't get the values.

Upvotes: 4

Views: 5524

Answers (2)

Steffan Martins
Steffan Martins

Reputation: 43

If lists have the same size:

<tr th:each="emp,iterStat : ${empList}">
    <td th:text="${emp.id}">ID</td>
    <td th:text="${accList[__${iterStat.index}__].field}">Emp Account</td> 
</tr>

Upvotes: 0

Nick
Nick

Reputation: 3961

Iterate over first list and get value from second list by index.

<tr th:each="emp,iterStat : ${empList}">
    <td th:text="${emp.id}">ID</td>
    <td th:text="${accList[iterStat.index].field}">Emp Account</td> 
</tr>.

Upvotes: 4

Related Questions