我有一個清單comments。每個評論都有一個屬性author。每個作者都有一個username.
只有當當前用戶還沒有寫評論時,我才想顯示一個特定的按鈕。(也就是說串列中不能有評論,其中作者的用戶名等于user.username)
這就是它在 JavaScript 中的樣子:
if(!comments.some(comment => comment.author.username === user.username))
但我不知道如何在 Thymeleaf 中做到這一點。我找不到任何必要的函式,如 array#map()、array#any() 等。行內回圈似乎也不是一回事。
<button th:if="${!<What goes here?>}" class="btn btn-primary">Add Comment</button>
我該怎么做?
uj5u.com熱心網友回復:
終于找到了一個可行的解決方案:https ://stackoverflow.com/a/34828280/10686377
使用 Spring 的Collection Selection,您可以檢查串列是否包含具有特定屬性值的元素。
<button th:unless="${comments.^[author.username == user.username]}" class="btn btn-primary">Add Comment</button>
uj5u.com熱心網友回復:
你可以試試這個會復制一些javascript的功能
<div th:each="comment : ${comments}">
<button th:if="${comment.author.username == user.username}" class="btn btn-primary">Add Comment</button>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/417596.html
標籤:
上一篇:當我有2個具有相同端點字串值的控制器方法時,為什么會出現“requestMappingHandlerMapping”錯誤
