Populate a html drop down list in play framework [

2019-03-01 10:28发布

问题:

Possible Duplicate:
Use of option helper in Play Framework 2.0 templates

Normal html code:

                        <select id = "game_duration">          
                        <option>01 hour</option>
                        <option>02 hour</option>
                        <option>03 hour</option>
                        <option>04 hour</option>
                        <option>05 hour</option>
                        <option>Never end</option>
                        </select>

To @select of Play framework...
I tried following Tutorial but it only printed plain html of the @select tag..
I'm new to play thus can anyone please help me?
Thank you very much.

回答1:

First import helper package(s) at the beginning of your view:

@import helper._

So you can use that sample:

@select(
   gameForm("game_duration"),
   options(Seq("01 hour","02 hour","03 hour","Never end")),
   '_label -> "Game duration", '_default -> "-- Select duration --"
)

Alternatively you can also use that code without previous importing helper package(s)

@helper.select(
   gameForm("game_duration"),
   helper.options(Seq("01 hour","02 hour","03 hour","Never end")),
   '_label -> "Game duration", '_default -> "-- Select duration --"
)

important: Try to use options(List("01 hour","02 hour","03 hour","Never end")) if Seq(...) version will fail while compiling.

btw, most probably it would be better using numeric values (ie int - easier to store and search in DB):

...
helper.options("60" -> "01 hour","120" -> "02 hour","180" -> "03 hour", "9999" -> "Never end"),
...

Also check this answer for more samples