How to fill a drop down list using the html helper "options" in a scala template

6,049 views
Skip to first unread message

Laurent Bois

unread,
Apr 5, 2012, 5:15:23 PM4/5/12
to play-fr...@googlegroups.com
Hi all,

In my Action i render the template like this : 

  return ok(

    views.html.event.render(Person.all(), Event.all(), eventForm)

  );


I display in the template

first line :

@(persons: List[Person], events: List[Event], eventForm: Form[Event])

a ) the List of events

b) a form for one event to create .

In this form, a select-options list (dropdown) , if possible loaded with the persons collection, as is :

         @select(

            field = eventForm("person_id"),

            options = persons,

            args = '_label -> "User:"

        )

But i get the compilation error : 

type mismatch; found : java.util.List[models.Person] required: Seq[(String, String)]

Any idea

Thanks for your help

Laurent


Sébastien RENAULT

unread,
Apr 5, 2012, 5:48:18 PM4/5/12
to play-fr...@googlegroups.com
Hy,


A Seq[(String, String)] is waited here, corresponding to the option & text value of the select.

Sebastien.

2012/4/5 Laurent Bois <lauren...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/ndUfpG06DzwJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Laurent Bois

unread,
Apr 6, 2012, 12:46:15 AM4/6/12
to play-fr...@googlegroups.com
Hi

Thanks for the link on github .
Still being not an expert in Scala, my question is :
I think we should then pass a Map between the controller action and the template.
1) How can i create this map from a query on a domain object "Person" (key : id , value : firstName + " " + lastName)
2) How to declare this map as input in the template 
@(persons: Map[Person, XX] (?), events: List[Event], eventForm: Form[Event]) 

3) How to write my select options with args being my input Map.

Laurent
Sebastien.

2012/4/5 Laurent Bois <lauren...@gmail.com>
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

Jörg Viola

unread,
Apr 7, 2012, 9:32:43 AM4/7/12
to play-fr...@googlegroups.com
funny, I just had to do this, consider:

persons.map(p => p.id -> (p.firstName + " " + p.lastName).toString)

Ah--- anyone knows how to get rid of the ().toString? - it complains about x+" "+y being a MutableBuffer instead of a String.

gautamnc

unread,
Apr 8, 2012, 9:33:31 PM4/8/12
to play-fr...@googlegroups.com
Hi,

I'm really a starter with Scala, so pardon me if I'm stupid here!
I'm stuck with a similar situation of yours, it seems. I want to populate a dropdown with distinct values(timesheetId here) and labels(a concat of 2 date values). So I did this.

@select(effortForm("timesheetId"),options(timesheets.map(aTimesheet=> aTimesheet.timesheetId.toString -> (aTimesheet.fromDate + " to " + aTimesheet.toDate))),'_label->"Choose Timesheet (*)")

But the page has a dropdown populated with same value (as a tuple) and label for options under select. 

Overloaded method value [apply] cannot be applied to (List[(java.lang.String, java.lang.String)])

I want to see this as result

<option value="1">Sun Apr 08 18:23:32 PDT 2012 to Sun Apr 08 18:23:32 PDT 2012</option>

Please help!

sun

unread,
Apr 9, 2012, 8:54:46 AM4/9/12
to play-framework
Not sure what options() does. Can you try it without it?

@select(effortForm("timesheetId")
, timesheets.map(_.timesheetId.toString -> (_.fromDate + " to " +
_.toDate))

gautamnc

unread,
Apr 9, 2012, 11:48:38 AM4/9/12
to play-fr...@googlegroups.com
Thanks, unfortunately it ended up with missing parameter type for expanded function ((x$2, x$3) => x$2.fromDate.$plus(" to ").$plus(x$3.toDate))

Found a solution, thanks to huynhjl at http://goo.gl/Cpzpm

Solution: @select(effortForm("timesheetId"),timesheets.map{ t =>t.timesheetId.toString -> (t.fromDate + " to " + t.toDate)},'_label- > "Choose Timesheet (*)")
Reply all
Reply to author
Forward
0 new messages