playframework使用@repeat获取索引?
在使用Play Framework twirl模板的地方,如何获取索引 我想要:playframework使用@repeat获取索引?,playframework,Playframework,在使用Play Framework twirl模板的地方,如何获取索引 我想要: @repeat(questionForm("correct"), min = 0) { correct => @b3.hidden( "correct[INDEX_HERE].id", correct("id").value, 'attr -> false ) } 但是我不知道怎么做。制作一个名为repeatWithIndex @(field:Field, min: In
@repeat(questionForm("correct"), min = 0) { correct =>
@b3.hidden( "correct[INDEX_HERE].id", correct("id").value, 'attr -> false )
}
但是我不知道怎么做。制作一个名为
repeatWithIndex
@(field:Field, min: Int = 1)(implicit f: (Field, Int) => Html)
@{
(0 until math.max(if(field.indexes.isEmpty) 0 else field.indexes.max + 1, min)).map(i => f(field("[" + i + "]"), i))
}
那么你可以像这样引用索引吗
@repeatWithIndex(questionForm("correct"), min = 0) { (correct, index) =>
@b3.hidden(s"correct[$index].id", correct("id").value, 'attr -> false )
}
repeatWithIndex
helper现在是框架的一部分。
根据本文,您可以获得与@tgk提供的解决方案类似的索引:
@helper.repeatWithIndex(myForm("emails"), min = 1) { (emailField, index) =>
@helper.inputText(emailField, '_label -> ("email #" + index))
}
下面是添加了repeatWithIndex
helper的拉取请求:
只需重新整理来源: