I want to write 4 columns in a row like this
Something Something Something Something Something Something Something Something
data sizes are dynamic, so it can be 4, 8 or more. this is archived in other template engine
{{#each list}} {{#if @index % 4 == 0}} {{/if}} {{this.name}} {{#if @index % 4 == 0}} {{/if}} {{/each}}
but how can I archive this in thymeleaf? I can't find the way because MODEL CODE THYMELEAF VIEW CODE RESULT I used an array to solve this problem . I think you will find a better way . I just created an account here to correct the accepted answer. The accepted answer works great so long as the "datas" being passed in is an array of consecutive integers. However, to make it work with any kind of data structure, "row.current" needs to change to "row.count", as follows: If you use EDIT: I have to further refine this because the accepted answer also does not work if the number of items in the list is not evenly divisible by 4. Here is a better (though probably not perfect) solution: This may be able to be refactored to eliminate one of the spans, but I have to move on now. th:each can be used on any element basically. So something like this: This can be done using th:each
is in tag(回答1:
List
回答2:
row.current
, then it uses the actual item in the list, which is great in the example shown, but not so great for any other kind of data structure. Hope this helps.回答3:
回答4:
numbers.sequence
too. Set colCount
to whatever number of columns you'd like: