0

I have loop in view tier of playframework application.

@for(art <- currentPage.getArticles()) {

}

Where method: getArticles() returns object of: List<ArticleI>.

I would like to get index value inside this loop.

How can I achive this?

I use Play 2.2.6 with Java and Eclipse IDE.

The eclipse plugin for play is very very bad. If it would work, I think that with intelisense I will figure this out on my own.

masterdany88
  • 5,041
  • 11
  • 58
  • 132

2 Answers2

0

You can get the index in this way:

@for((art, index) <- currentPage.getArticles()) {
  <p>Element @index is @art</p>
}

Note: The index starts with 0, so if you want to display a 1 instead:

@for((art, index) <- currentPage.getArticles()) {
  <p>Element @{index+1} is @art</p>
}

I hope, that helps you...

Sim
  • 3,279
  • 1
  • 17
  • 23
0

Use @index

@for((art, index) <- currentPage.getArticles()) {
    <li>Item @index is @art</li>
}
Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46
  • Doesn't work. I am getting an error: `Index.scala.html:26: constructor cannot be instantiated to expected type; [error] found : (T1, T2) [error] required: cms.models.i.ArticleI [error] @for((art, index) <- currentPage.getArticles()) { [error] ^ [error] one error found [error] (compile:compile) Compilation failed ` – masterdany88 Sep 21 '15 at 07:19