Issue
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?>}" class="btn btn-primary">Add Comment</button>
How do I do it?
Solution
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]}" class="btn btn-primary">Add Comment</button>
Answered By - FireFuro99
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.