Anil Kumar Pandey
Anil Kumar Pandey

Reputation: 1927

Play framework: How do I print index number inside scala template by using map

Hi I am pretty new in play framework and scala, continue I am reading the play documentation but I am facing problem while printing the index inside the map in the scala template file. I have tried below code but it was not working for me.

//Attempt 1: But not working

@(customer: Customer, orders: Seq[Order])
<h1>Welcome @customer.name!</h1>

<ul> 
@orders.map { case(index,order) =>
  <li>@index</li>
  <li>@order.title</li>
} 
</ul>

//Attempt 2: But not working

@(customer: Customer, orders: Seq[Order])
<h1>Welcome @customer.name!</h1>

<ul> 
@orders.map { order =>
  <li>@order.index</li>
  <li>@order.title</li>
} 
</ul>

Please give me some solution for this or give something other reference/resource link for play where I can explore more. You can find the above example from play documentation.

Upvotes: 0

Views: 676

Answers (1)

Akos Krivachy
Akos Krivachy

Reputation: 4966

You can use zipWithIndex. It takes a list and creates a tuple from it where the first part is the element of the list and second is the index.

Example:

@orders.zipWithIndex.map { case (order, index) =>
  <li>@index</li>
  <li>@order.title</li>
} 

Upvotes: 6

Related Questions