Playframework: List Quoting

I have a list that I pass to the view.

public static Result index() { List<String> list = new ArrayList<String>(); list.add("idea 1"); list.add("idea 2"); list.add("idea 3"); list.add("idea 4"); list.add("idea 5"); list.add("idea 6"); list.add("idea 7"); return ok(index.render(list)); } 

I would like to iterate over it 3 at a time so that it displays like this:

 <ul> <li>idea 1</li> <li>idea 2</li> <li>idea 3</li> </ul> <ul> <li>idea 4</li> <li>idea 5</li> <li>idea 6</li> </ul> <ul> <li>idea 7</li> </ul> 

I cannot figure out how to do this using a for loop.

I have Java code for this, just can't translate it into a template for a playback platform template:

  int size = list.size(); int loopSize = (int) Math.ceil(size / 3.0); int counter = 0; for(int j = 0 ; j < loopSize; j++) { System.out.println("---------------------"); for (int i = 0; i < 3; i++) { if(counter < size) { System.out.println(list.get(counter)); counter++; } else { break; } } System.out.println("---------------------"); } 
+6
source share
3 answers

This should work:

 @(list: List[String]) @for(index <- 0 until list.size){ @if(index % 3 == 0){ <ul> } <li>@list(index)</li> @if(index % 3 == 2 || index == (list.size - 1)){ </ul> } } 
+10
source

Here's a more Scala version:

 @(list: List[String]) @list.grouped(3).map { group => <ul> @group.map { item => <li>@item</li> } </ul> } 

(Another answer answers the question asked in more detail, in particular, since the questionnaire cannot use Scala in the rest of its project).

+8
source

Here is a working example,

 @(list: List[String]) @for(value <- list){ } 
+1
source

Source: https://habr.com/ru/post/946446/


All Articles