简体   繁体   中英

playframework - how to set a select input in scala

So I have a form on a page, and I am trying to pre load all the old value when the page loads up. But I am having some trouble achieving this for the select object.

I have the following scala code for generating the select:

<select id="networkInterfaces">
            <option value="">First Available</option>
            @for(interface <- networkInterfaces) {
                @if(interface.Name == configs.last.networkInterfaceName) {
                    <option selected="selected" value="@interface.Name">@interface.DisplayName</option>
                } else {
                    <option value="@interface.Name">@interface.DisplayName</option>
                }
            }
        </select>

And when the page loads it does show that the selected network interface is selected. But the problem is if I change some of my other settings and submit it is returning the options html not the value. Is there a way to select the form value for the select in scala while the page is loading? Is there something fundamental I am missing? Otherwise I will change the way it is handled to process display name rather than value...

EDIT

As I could not get this to work i changed the value to @interface.DisplayName and then converted it in code on the server. I would like to be able to do it properly but it doesnt seem to work.

Is that correct that <select id="networkInterfaces"> has no name specified?

and than you should use play helper @select and Form object. I think its the better way to handle form values.

@(form : Form[ABean])
@import helper._

@select(
form("configs.last.networkInterfaceName"),
options(networkInterfaces),
'_label -> "Interface Name",
'_default -> "First Available"
)

Further info about helper @select visit : https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/views/helper/select.scala.html

Try this way.

    <input type="hidden" id="groupIdRef" name="groupIdRef" value="@userRecord.groupId">
    @input(field=userRecordform("groupId"),'_label -> "") { (id, name, value, htmlArgs) =>
        <select id="groupId" name="groupId" @toHtmlArgs(htmlArgs)>
            options.map { v =>
                @for(g <- groups){
                  <option value=@g.id >@g.displayName</option>
                }
            }
        </select>
    } 


// User JavaScript this onload function
window.onload = function() {    
document.getElementById('groupId').value = document.getElementById('groupIdRef').value;
}

It is work for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM