Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in thymeleaf, how can write th:each to combine rows and columns?

Tags:

thymeleaf

I want to write 4 columns in a row like this

<div class="row">
   <div class="span3">Something</div>
   <div class="span3">Something</div>
   <div class="span3">Something</div>
   <div class="span3">Something</div>
</div>
<div class="row">
   <div class="span3">Something</div>
   <div class="span3">Something</div>
   <div class="span3">Something</div>
   <div class="span3">Something</div>
</div>

data sizes are dynamic, so it can be 4, 8 or more. this is archived in other template engine

{{#each list}}
  {{#if @index % 4 == 0}}
    <div class="row">
  {{/if}}
  <div class="span3">{{this.name}}</div>
  {{#if @index % 4 == 0}}
    </div>
  {{/if}}
{{/each}}

but how can I archive this in thymeleaf? I can't find the way because th:each is in tag(<div class="row"> or <div class="span3">) as attribute.

like image 451
Outsider Avatar asked Jul 23 '13 13:07

Outsider


People also ask

How do you use Thymeleaf th text?

The Thymeleaf th:text tag will replace all the text in your h1 tag, that is the reason your output only shows "TITLE". You should place the <small> tags outside your h1 tag.

How do you add a row in Thymeleaf?

Declare a function to add an extra row. This will be bound to the “Add” button using @click="addExtraTeamPlayerForm()" . Get the <div> with the teamplayer-forms id since we will add a new row there. Get the current count, which is added as an attribute when Thymeleaf renders the page.

How do you display strings in Thymeleaf?

In this case, you should use th:text . For example, Username: <p th:text=${account. username}>Username will be rendered here</p> .

What is th each in HTML?

4. The th:each Attribute. In Thymeleaf, iteration is achieved by using the th:each attribute. One of the interesting things about this attribute is that it will accept and iterate over some different data types, such as: objects implementing java.


2 Answers

Model code

List<String> data = new ArrayList<String>();
data.add("1");
data.add("2");
data.add("3");
data.add("4");
data.add("5");
data.add("6");
data.add("7");
data.add("8");

model.addAttribute("datas", data);

Thymeleaf view code

<div th:each="data, row: ${datas}" th:with="numList=${#strings.listSplit('3,2,1,0', ',')}" th:if="${row.current} % 4 == 0" class="span3">
    <span th:each="num : ${numList}" th:with="dataIndex=(${row.index} - ${num})" th:text="${datas[dataIndex]}">data</span>
</div>

Result

<div class="span3">
    <span>1</span><span>2</span><span>3</span><span>4</span>
</div>
<div class="span3">
    <span>5</span><span>6</span><span>7</span><span>8</span>
</div>

I used an array to solve this problem. I think you will find a better way.

like image 85
Younghan Kim Avatar answered Apr 29 '23 11:04

Younghan Kim


This can be done using numbers.sequence too. Set colCount to whatever number of columns you'd like:

<th:block th:with="colCount=${4}">
    <div th:each="r : ${#numbers.sequence(0, datas.size(), colCount)}"  class="row">
        <div th:each="c : ${#numbers.sequence(0, colCount - 1)}" th:if="${r + c &lt; datas.size()}" th:text="${datas.get(r + c)}" class="span3"></div>
    </div>
</th:block>
like image 40
MNos Avatar answered Apr 29 '23 12:04

MNos