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.

Answers


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

Need Your Help

Display stars from a value

html css

I've followed this awesome tutorial for achieve a stars rating system : Tutorial Demo

finding long repeated substrings in a massive string

performance algorithm string search

I naively imagined that I could build a suffix trie where I keep a visit-count for each node, and then the deepest nodes with counts greater than one are the result set I'm looking for.