Thymeleaf:如何以相反顺序遍历列表? [英] Thymeleaf: How to loop through a list in inverse order?

查看:1240
本文介绍了Thymeleaf:如何以相反顺序遍历列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"notifications"的列表,我使用Thymeleaf的每种方法"来逐一访问其元素.我可以成功完成此操作,如下所示.

I have a list called 'notifications' and I use Thymeleaf 'each method' to access its elements one by one. I can do this successfully as below.

<li th:each="n : *{notifications}"> 
    <h4 type="text" th:text="*{n.message}"></h4>
</li>

注意:消息"是我需要从列表中检索的属性.

note: 'message' is the attribute I need to retrieve from the list.

如何以相反的顺序访问元素?例如,如果这是我当前的输出,

How can I access the elements in the reverse order? For an example if this is my current output,

Cat
Dog
Rat

如何获得这样的输出?

Rat
Dog
Cat

推荐答案

如果可能的话,我会在服务器端将其反转.如果您不想这样做,那么可能会适合您:

I would reverse it server side, if possible. If you don't want to do that, maybe something like this would work for you:

<li th:each="i : ${#numbers.sequence(notifications.size() - 1, 0, -1)}"> 
    <h4 type="text" th:text="${notifications[i].message}"></h4>
</li>

这篇关于Thymeleaf:如何以相反顺序遍历列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆