需要在 scala.html 中使用计数器变量并使用 @for(i <- list){@(a++)} 显示

Need to use counter variable in scala.html and display using @for(i <- list){@(a++)}

play-framework-twirl-template

1.我在变量列表中有多个问题。 2.需要在for循环外声明变量,在for循环内作为计数器变量使用。 像@var count=0; 2. 我在 scala.html 文件上迭代列表 @for(d <-list){ count++ 或 count+=1;// 出错 } 3.通过这个循环需要打印带问题的数字 像 Sr.No QuestionName 选项.. 1 ABC A.. 2 ZYX D..

您应该在 Scala 集合上使用 zipWithIndex 功能(更多信息请参见 Scala 文档:here);

使用示例:

@for((value, index) <- list.zipWithIndex){

  @* Print value *@
  @value 

  @* Print index - first index will be 0 *@
  @index

  @* Print index + 1 *@
  @{index+1}

}