th:if inside th:each

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

th:if inside th:each

bigal.nz
Hello,

I want to get a row for each surname of a person in a list, like:

Surname 1
Surname 2
Surname 3

I have tried :

<tbody>
    <tr th:each="personsList,itrStat : *{persons}">
        <td th:text="${personsList.person.surname} + ' ' +${itrStat.index}"></td>
    </tr>
</tbody>

Which produces :

surname 0 surname 1 surname 2

I also tried :

<tbody>
<tr th:each="personsList,itrStat : *{persons}">
	<td th:text="${personsList[__${itrStat.index}__].person.surname} + ' ' +${itrStat.index}"></td>
</tr>
</tbody>
 
Which gives an error : org.springframework.expression.spel.SpelEvaluationException: EL1027E: Indexing into type 'com.example.domain.EpisodePerson' is not supported

Anyone able to help please?

Thanks

Al
Reply | Threaded
Open this post in threaded view
|

Re: th:if inside th:each

Metroids
View the source in a browser after running it through thymeleaf, what does the html look like?

(The thymeleaf looks correct to me.)
Reply | Threaded
Open this post in threaded view
|

Re: th:if inside th:each

bigal.nz
Hi Metroids,

Thanks for the reply. I got to the bottom of it, I was making a dumb mistake:

<tbody>
<tr th:each="personsList,itrStat : *{persons}">
	<td th:text="${personsList[__${itrStat.index}__].person.surname} + ' ' +${itrStat.index}"></td>
</tr>
</tbody>

My test code above wasn't in a set of table tags..... duh.

For the benefit of anyone else after getting the itrStat.index I wanted to apply it to a input, which only binds to the form backing object, so when looping over the inputs I have to use:

th:field="*{persons[__${itrStat.index}__].person.surname}"

Note the * as the $ operator on PersonsList wouldnt work as its not the backing object.

HTH & Thanks

Al