Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf th:each adding coma between elements

I have collection X

I iterate through it and write it like that:

<span th:each="a, stat : ${X}"
      th:text="${X[__${stat.index}__].someProperty} + ','">
</span>

my other try was:

<span th:each="a, stat : ${X}" th:for="|a${stat.index}|"
      th:text="${X[__${stat.index}__].someProperty} + ','">
</span>

unfortunately the output is the same.

the output in the span is:

test1, test2, test3,

I want the output to be:

test1, test2, test3

without the comma at the end. How can I achieve that?

Solution:

  1. Beware of The value of attribute th:text associated with an element type span must not contain the '<' character.

Code:

<span th:each="a, stat : ${X}"
      th:text=" ${X[__${stat.index}__].someProperty} +  (${stat.size-1 > stat.index}? ',':'') ">
</span>
like image 819
Kamil Witkowski Avatar asked Mar 18 '16 14:03

Kamil Witkowski


1 Answers

Thymeleaf has an iteration property last see the documentation here: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

use

<span th:each="a, iterStat : ${X}" th:text="!${iterStat.last} ? ${a} + ',': ${a}"></span>
like image 72
ndrone Avatar answered Sep 21 '22 13:09

ndrone