问题
I am having big troubles get Multiselect to work with my Play 2.0 application.
I have tried different solutions that I found on google, but none works for 2.0.1.
Do you have any guides or tips for getting multi select to work ?
The html...
<select multiselect="multiselect" ... name="groupIds[]"> ... </select>
The Form
class UserAdminForm{
public Long[] groupIds;
}
and later in request handler...
Form<UserAdminForm> form = uform.bindFromRequest(); // Bam , [NumberFormatException: For input string: ""]
Are there any good way of dealing with POST array ?
回答1:
I had the same problem, I thing multiselect form helper is a bug in Play 2. Any way, I'd fixed it by renaming the select as @name[]. So you create a template for ex. selectMultiple.scala.html containing this code:
@(field: play.api.data.Field, options: Seq[(String,String)], args: (Symbol,Any)*)(implicit handler: helper.FieldConstructor, lang: play.api.i18n.Lang)
@values = @{ field.indexes.map { v => field("[" + v + "]").value } }
@helper.input(field, args:_*) { (id, name, value, htmlArgs) =>
<select id="@id" name="@(name)[]" @toHtmlArgs(htmlArgs) multiple="multiple">
@args.toMap.get('_default).map { defaultValue =>
<option class="blank" value="">@defaultValue</option>
}
@options.map { v =>
<option value="@v._1" @{if(values.contains(Some(v._1))) "selected" else ""} >@v._2</option>
}
</select>
}
Having in your model a list for mapping the component, you use this template in your html page like:
@selectMultiple(
myForm("groupsId"),
myOptions,
'_label -> "My MultiSelect"
)
Hoping this help you! (note that I'm using Play for Scala)
回答2:
You can create a template like the following :
@(field: play.api.data.Field, options: Seq[(String,String)], args: (Symbol,Any)*) (implicit handler: FieldConstructor, lang: play.api.i18n.Lang)
@values = @{ field.indexes.map { v => field("[" + v + "]").value } }
@input(field, args:_*) { (id, name, value, htmlArgs) =>
<select id="@id" name="@name" @toHtmlArgs(htmlArgs) multiple="multiple">
@options.map { v =>
<option value="@v._1" @{if(values.contains(Some(v._1))) "selected" else ""}>@v._2</option>
}
</select>
}
You can find this example at play-framework discussion group
回答3:
You error is:
NumberFormatException: For input string: ""
This means that you are receiving an empty String, which can't be turned into a number. Better mark the field in the Form as optional if it's possible to not get a value in that field.
回答4:
Please see my example below play 2.2
The options for the multi-select dropdown are in the data HashMap
@(data:HashMap[String, HashMap[String,String]])
@import helper._
@select(field = myform("options"), options = data.get("options").toSeq,
'_label -> "My Options*", '_showConstraints -> false, 'class ->"required",
'id->"options", 'multiple->"multiple")
来源:https://stackoverflow.com/questions/10502490/multiselect-select-play-2-0