Scala : clever suggestion to build an HTML select option list with Scala

395 views
Skip to first unread message

Nicolas Martignole

unread,
Aug 5, 2011, 11:11:51 AM8/5/11
to play-fr...@googlegroups.com
Hi Players

This is a question about Scala.

My customer would like a select/drop-down list on its register page
so that a new visitor can enter its birthdate. The customer will have
to select first the day in a combo-box, then the month and its year...

To generate a select/option with a list of days, I use this template :
It works, but I think it might not be the best solution

            <select name="dobDay" id="dayOfBirth">
               @((1 to 31 toList).map{e=>format.raw("<option value='"+e+"'>" + e +"</option>")})
            </select>

Do you have a better suggestion ?

This does not work
     <select name="dobDay" id="jourNaissance">
               @(1 to 31 toList).map{ e=>
                   <option value="@e">@e</option>
                }
            </select>
(I get a "not found: value e")

Thanks

Nicolas Martignole

Nicolas Martignole

unread,
Aug 5, 2011, 11:54:48 AM8/5/11
to play-framework
Answering to myself...

I created a custom tag and it's a bit simplier.
I also implemented the case where the validation failed, the page is
reloaded and we need to select the previous value.

In the main page I declared my tag as shown below :

<select name="dobDay" id="jourNaissance">
@generateOption(31,flash.get("dobDay"))
</select>

In the generateOption.scala.html I wrote this :

@(rangeParam:Int,currentValue:String)
@((1 to rangeParam toList).map{e=>
if(currentValue!=null && e.equals(Integer.parseInt(currentValue)))
{
format.raw("<option selected value='"+e+"'>" + e +"</option>")
} else {
format.raw("<option value='"+e+"'>" + e +"</option>")
}
}
)

Hope it will also help someone with Scala, html select, option and
selected

Nicolas Martignole

Sadache Aldrobi

unread,
Aug 5, 2011, 12:44:45 PM8/5/11
to play-fr...@googlegroups.com
<select name="dobDay" id="dayOfBirth">
               @Range(1,31).map{e =>
               <option value='@e'>@e</option>)
               }
            </select>

Your problem is the parentheses that will declare all what in between as code and the @ should be followed by a method call.


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
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.



--
www.sadekdrobi.com
ʎdoɹʇuǝ

felipeko

unread,
Aug 5, 2011, 2:58:30 PM8/5/11
to play-fr...@googlegroups.com
If you're not going to reuse this template anywhere else you could declare it on the same html using Reusable Blocks

Xuefeng Wu

unread,
Aug 5, 2011, 8:19:13 PM8/5/11
to play-fr...@googlegroups.com
Good idea. The scala template can not parse parentheses correctly which without ident.

在 2011年8月6日星期六UTC+8上午12时44分45秒,Sadache写道:
Reply all
Reply to author
Forward
0 new messages