scala - Passing selected value of a select to form action -


i have menu want differ based on account selected in system.

i have page allows user select account html select. when user submits form account selection page want call menu method on controller passing in selected value url looks correct.

here existing template page allows user select account:

@helper.form(action = routes.accounts.menu {     <table>         <tr>             <td><select id="accountnames">                 @accountnames.map { name =>                     <option value="@name">@name</option>                 }             </select></td>         </tr>         <tr>             <td>                 <p>                     <input type="submit" value="choose">                 </p>             </td>         </tr>     </table> } 

from routes file:

get /account/:accountname/menu controllers.accounts.menu(accountname: string) 

how reference selected value select (id="accountnames") , pass form action?

actually think you're on wrong side doing that.

if form's action has change on use of 'select', has done using js. when form submitted (event submit) have update url.

this can done using javascriptroutes.

so have several things:

1/ create javascriptrouter (assuming add in application.scala)

def javascriptroutes = action {   ok(     routes.javascriptrouter("playroutes")(       //accounts       controllers.routes.javascript.accounts.menu     )   ).as("text/javascript")  } 

2/ define in routes file

# javascript routing     /assets/javascripts/routes      controllers.application.javascriptroutes 

3/ add related javascript file import in views, let in main.scala.html

<script type="text/javascript" src="@routes.application.javascriptroutes"></script> 

4/ add submit handler form before executing default behavior

$("form").submit(function () {     //this computes correct url giving parameter value of selected option     var newurl = playroutes.controllers.accounts.menu($("#accountnames").val()).url     $(this).attr("action", newurl);  }) 

notice how we've used playroutes both in controller (1) , js call (4).


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -