C_B
C_B

Reputation: 2682

Thymeleaf: append to html element / avoiding repetitive iteration

I'm looking for a way to dynamically append html elements to other html elements using Thymeleaf.

Consider the following:

<ol class="ol1" />
<ol class="ol2" />
<ol class="ol3" />

<iterate th:each="model">
  <!-- ol1.append(model.name) -->
  <!-- ol2.append(model.type) -->
  <!-- ol3.append(model.something) -->
</iterate>

I know that a possible solution would be to have 3 loops, one nested in each of the <ol> tags like so:

<ol class="ol1">
  <iterate  th:each="model">
    model.name
  </iterate>
</ol>
<ol class="ol2">
  <iterate  th:each="model">
    model.type
  </iterate>
</ol>
<ol class="ol3">
  <iterate  th:each="model">
    model.something
  </iterate>
</ol>

This above solution is too inefficient for my liking.

So, does Thymeleaf provide the functionality to perform only one iteration in this situation?

Upvotes: 2

Views: 542

Answers (1)

Crunch
Crunch

Reputation: 510

Sorry, there's no simpler or more efficient way to do it. I started to think about other ways to get this accomplished, and none were better than the three loops you are using in your question.

Upvotes: 1

Related Questions