[英]Conditional layout of multiple divs in Thymeleaf
I am struggling to format this div structure using th:each
in Thymleaf. 我正在用Thymleaf中的th:each
格式化这个div结构。 The desired html format is as follows 所需的html格式如下
Desired HTML 所需的HTML
<div class="row interest-featured"> <!--Parent Div -->
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
</div>
<div class="row interest-featured"> <!--Parent Div -->
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
</div>
Progress until now 到现在为止
<div th:each="interest,status : ${interest}" th:class="${(status.index + 1 % 3) == 0}? 'row interest-featured' :''">
<div class="col-md-4 interest">
</div>
</div>
Any help would be highly appreciated. 任何帮助将不胜感激。 Thanks 谢谢
PS: HTML texts are removed for brevity PS:为简洁起见,删除了HTML文本
I think I understand the issue here. 我想我明白这里的问题。 If you have a flat list and you want to display it in a Bootstrap grid, you'll have to create new rows for each n elements. 如果您有一个平面列表,并且想要将其显示在Bootstrap网格中,则必须为每n个元素创建一个新行。
The solution to this problem isn't as clean as a normal th:each
, but you'll have to use it twice and have proper th:if
statements on when you want to display it. 解决此问题的方法并不像普通的th:each
那样干净,但是您必须两次使用它,并在要显示它时使用正确的th:if
语句。
For example, for the .row
element, you want to only show it for elements with index 0, 3, 6, ... (if you want a 1/3th column grid). 例如,对于.row
元素,您只希望对索引为.row
元素显示(如果要使用第1/3列网格)。 This means you should do: 这意味着您应该:
<div th:each="interest,rowStatus : ${interests}" class="row interest-featured" th:if="${rowStatus.index % 3} == 0">
</div>
Now, for the children of the row you'll have to iterate again over your collection, but filter it so that for the first row you only show elements with index 0-2 for the second row the elements 3-5, ... . 现在,对于该行的子级,您将不得不再次遍历集合,但是对其进行过滤,以便对于第一行,您仅显示索引为0-2的元素,第二行显示3-5,...的元素。 。
To do that you use another th:if
: 为此,请使用另一个th:if
:
<div th:each="interest,rowStatus : ${interests}" class="row interest-featured" th:if="${rowStatus.index % 3} == 0">
<div th:each="interest,interestStatus : ${interests}" class="col-md-4 interest" th:text="${interest}"
th:if="${interestStatus.index lt rowStatus.index + 3 and interestStatus.index ge rowStatus.index}"></div>
</div>
If you don't want to use a cumbersome template like this, you can always just iterate over the column itself. 如果您不想使用此类繁琐的模板,则始终可以仅遍历该列本身。 If you're using Bootstrap, it will automatically create columns that do not fit within a row on a new line, for example: 如果您使用的是Bootstrap,它将自动创建新行中不适合的列,例如:
<div class="row">
<div class="col-md-4">Column 1</div>
<div class="col-md-4">Column 2</div>
<div class="col-md-4">Column 3</div>
<div class="col-md-4">Column 4</div><!-- This item will be on a new row because the grid does only allow 12 spaces -->
</div>
However, there are some differences between both approaches when the content of the column is variable in height. 但是,当列的内容高度可变时,两种方法之间存在一些差异。
NOTE : You're also using the
interest
variable twice. 注意 :您还将两次使用interest
变量。 The collection is named${interest}
, but the result variable is also calledinterest
. 该集合名为${interest}
,但结果变量也称为interest
。 You probably have to rename one of those. 您可能必须重命名其中之一。
You use object. 您使用对象。
html HTML
<div class="source_list" th:each="history : ${historys}">
<label>date : <span th:text="${history.date}">2016.06.27</span></label>
<br />
<label>description : </label><span th:text="${history.description}">2016.06.27</span>
<br /> <br />
</div>
contorller contorller
@RequestMapping(value = "/settings/history/{companyIdx}")
public ModelAndView showHistory(ModelAndView mav, @PathVariable int companyIdx, HttpServletRequest request) {
String language = CookieUtil.getCookieValue(request, "lang");
Company company = companyService.findCompanyByIdx(companyIdx);
List<CompanyHistory> historys = companyService.findHistoryByLanguage(company, language);
mav.addObject("company", company);
mav.addObject("historys", historys);
mav.setViewName("/company/company_settings_history");
return mav;
}
dto DTO
@Entity(name = "company_history")
public class CompanyHistory {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int idx;
@ManyToOne
@JoinColumn(name = "company_id")
private Company company;
private String date;
private String description;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.