-
-
Save gakuzzzz/3824646 to your computer and use it in GitHub Desktop.
repeat helper with index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package views.html.helper | |
import play.api.templates.Html | |
import play.api.data.Field | |
object repeatWithIndex { | |
def apply(field: Field, min: Int = 1)(f: (Field, Int) => Html) = { | |
val idSeq = if (field.indexes.size >= min) { | |
field.indexes | |
} else { | |
val diff = min - field.indexes.size | |
val maxIndex = if (field.indexes.isEmpty) 0 else field.indexes.max + 1 | |
field.indexes ++ (maxIndex until (diff + maxIndex)) | |
} | |
idSeq.map(i => f(field("[%d]".format(i)), i)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment