Let's see if I understand your problem:
You have a model where employees are assigned to departments. According to your description of the problem, the department object has a list of employees assigned to it.
From what I saw, you are selecting a collection of departments from your controller class into your vision.
Thymeleaf allows you to use iteration in elements through th: each attribute, as you already know. You can slightly reduce your code by iterating only in a collection of employees, for example:
<table> <tr th:each="employee, empStatus : ${department.employees}">
NOTE If I'm not mistaken, the result of your template will be repeated due to the number of departments.
This way your code will be cleaner and you can just focus on working with employees.
In connection with the basic question of defining an index for each row, you can use something known as โiteration statusโ.
I think you already know something about this, but I will try to explain my path that can enlighten something:
According to Thymeleaf's documentation, you can use a variable provided by Thymeleaf that will give you access to some iteration information. Using this variable, you can collect some information whose properties are given in the Thymeleaf Documentation (check session 6.2):
The current iteration index starting at 0. This is an index property. The current iteration index starting at 1. This is count . The total number of elements in the iterated variable. This property is size . Iteration variable for each iteration. This is the current property. Whether the current iteration will be even or odd. These Boolean properties are even / odd . Will the current iteration be the first. This is the boolean property of first . Is the current iteration last. This is last boolean.
To access this variable, you need to declare it shortly after declaring an element that represents each iteration, as you did before:
<tr th:each="employee, empStatus : ${department.employees}">
You can also omit the declaration of this variable and still use it. This is only possible because Thymeleaf makes this expression to you, where it takes the name given to the variable representing each element of the iteration (in this case "employee"), and adds the suffix "Stat" to it (as a result of "employeeStat").
You will have access to this variable and its internal properties only inside the code fragment defined by the tag using th: each attribute.
If you know how to display rows and columns on the page of your web application, all you have to do is use the properties of the iteration state variable, for example:
<td th:text="${empStatus.index}"></td>
Or if you prefer:
<td th:text="${empStatus.count}"></td>
If you have any questions, please comment and I will edit my answer to a better understanding.
EDIT
Oh, I understand only now that your real problem is, sorry.
If you want to create a list in which the name of each employee should appear, followed by the name of your respective department, and each list item with a different number (all ordained), then the problem is quite simple.
All you need to do first is to go through all the departments, showing the name of the employee, followed by the name of the department, as you wish. This will solve part of the problem, as this is a simple matter of logic and using iteration.
In order for this to happen on a regular basis, namely, with each record / element listed in order, you can, for example, create a local variable, one of the other resources in Thymeleaf.
To quickly learn about setting up and using this type of resource, check out this link: setting the value for the variable name in the timeline
Or you can check the Thymeleaf documentation (Session 9): http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#local-variables
After creating your variable, try using it as a counter, where it will be incremented for each record.
I hope this helps and solves your problem. If this does not work, try again and I will try another solution. Similarly, if this is not your question, comment as well.
Note : Again, I ask you to be careful in your loop structure. You can be creating a repeating structure that creates several table structures in the HTML code, which, I believe, is not the way you want. Also, make sure that you are performing arithmetic operations correctly. I believe that your increase operation should be inside the curly braces {}, and not from them.
EDIT 2
I searched all over the place to do this ... But, unfortunately, I could only find this:
Counters in a cycle in Timeleaf
You can find this in the documentation of Thimeleaf (Appendix B - Numbers). This is a valid way to do a loop with Thymeleaf. However, it is difficult to make a nested loop, because we are working in Thymeleaf with attributes, and not with tags / HTML elements. If you created a new line only inside a nested loop, then this is not possible. Perhaps you can try to use the "th: each" attribute twice, in the same "tr" tag, for example:
<tr th:each="" th:each""> </tr>
Although I believe that this will not work either ...
I really apologize, Maciej, as this is not the final answer, and therefore, not very helpful. However, I ask you to stop just a minute and try to browse your web application. Think about it, you want to create a table by ordering things, listing everything with numbers. Wouldn't it be better to do this in Java code? Moreover, are you trying to do this in SQL? Could you get the sorted database results with SQL? Understand that you are doing the work of the organization here, where each employee in each department will be listed and the departments in order.
You see, Iโm not saying that you are doing it wrong, but in these circumstances, and if we stop analyzing, developers usually get ordered results from the business level of the application, and we allow the presentation level to be responsible for displaying them.
If this is not possible in your application, and the link that I sent you now can not direct you to solve your problem, then I am very sorry, but I can not do anything. Maybe it's time to ask about it on the Thymeleaf forum, where its developers are likely to help you (perhaps):
http://forum.thymeleaf.org/General-Usage-f2234430.html
I sincerely hope that you can succeed in solving your problem. And if you do, please, I ask you to share your decision with everyone here, as many people may one day find themselves in the same situation.
Atรฉ depois, e boa sorte amigo.