0
votes

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.

1

1 Answers

5
votes

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>
}