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

How do you access your images folder from a controller?

ruby-on-rails ruby file prawn

I'm setting up Prawn right now and for one param it wants to know the img location.

Combine ggplots but fix the size/ratio of the plots

r ggplot2

I have two plots that I combine. arrangeGrob() squeezes them so that the size of the new image is the same as one alone. How can I arrange them while preserving the ratio/size?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.