0

I am new to the play framework and scala templates. I have to loop through a map, and create an input tag for each element, with ascenting tag id. So what I want to have in my resulting html is something like the following:

<li><input id="option1" type="checkbox"/><label for="option1">sometext</label></li>
<li><input id="option2" type="checkbox"/><label for="option2">sometext</label></li>
<li><input id="option3" type="checkbox"/><label for="option3">sometext</label></li>

The amount of inputs is dynamic, and their id needs to be ascending.

This is what I currently have, just looping through the map:

@for(c <- frage.getAllChoices){
    <li><input id="option1" type="checkbox"/><label for="option1">@c.getText()</label</li>
}

I tried using #define, but i can't change the value afterwards: it just prints the expression. And afaik creating a variable with "var" is not possible in play scala templates.

4

1 回答 1

1

实际上,在您的情况下,它应该是对象的 ID,而不仅仅是递增的值,不是吗?

无论如何,如果您只需要增加值,则可以使用zipWithIndex ,如此处所示,

@for((c, index) <- frage.getAllChoices.zipWithIndex) {
    ...
}
于 2014-05-06T07:45:34.353 回答