I have a list of comments. Each comment has an attribute author. And each author has a username.
I want to display a specific button only if the current user did not write a comment yet. (Which means there must be no comment in the list, where the username of the author equals user.username)
This is what it would look like in JavaScript:
if(!comments.some(comment => comment.author.username === user.username))
But I have no idea, how to do it in Thymeleaf. I could not find any necessary functions like array#map(), array#any(), etc. Inline loops also don't seem to be a thing.
<button th:if="${!<What goes here?>}" >Add Comment</button>
How do I do it?
CodePudding user response:
You can try this is will replicate some function of javascript
<div th:if="comment : ${comments}">
<button th:if="${comment.author.username == user.username}" >Add Comment</button>
</div>
CodePudding user response:
Finally found a working solution: https://stackoverflow.com/a/34828280/10686377
Using Spring's Collection Selection you can check if a list contains an element with a specific property value.
<button th:unless="${comments.^[author.username == user.username]}" >Add Comment</button>
