2017-01-19 47 views
1

Play Framework twirlテンプレートを使用しているインデックスを取得するにはどうすればよいですか?playframework @repeatを使用してインデックスを取得しますか?

私が持っていると思います:

@repeat(questionForm("correct"), min = 0) { correct => 
    @b3.hidden("correct[INDEX_HERE].id", correct("id").value, 'attr -> false)   
} 

しかし、私はどのように行うのか分かりません。

答えて

2

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)   
} 
+1

だけのソースをオフにリフ:

@helper.repeatWithIndex(myForm("emails"), min = 1) { (emailField, index) => @helper.inputText(emailField, '_label -> ("email #" + index)) } 

ここではrepeatWithIndexヘルパーを追加プル要求です。 com/playframework/playframework/blob/master/framework/src/play/src/main/scala/views/helper/Helpers.scala#L103 – tgk

+0

ありがとう、woooo! – bharal

関連する問題