Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating @select dropdownlist in java

I'm using play framework 2.0.4

In my java file,

return ok(views.html.name.render(Name.all(),NameForm));


In my html file,

@(name: List[Name],NameForm: Form[Name])

I want to make a dropdownlist (like using select, option tags in plain HTML)by on data from name array by using @select in @import helper._
I'm quite new to Play thus can someone show me how can I archive this?
Thank you very much.

like image 663
Xitrum Avatar asked Mar 13 '26 12:03

Xitrum


1 Answers

One way to do it is by defining your options as a list, returned by a static method

Create a Java class

public class ComboboxOpts {
    public static List<String> myCustomOptions(){
        List<String> tmp = new ArrayList();

        tmp.add("This is option 1");
        tmp.add("This is option 2");
        tmp.add("This is option 3");
        return tmp;
    }
....
}

In your HTML, import the helper

@import helper._

and try

 @select(
     myForm("myDropdownId"),
     options = options(ComboboxOpts.myCustomOptions),
     '_label -> "This is my dropdown label",
     '_showConstraints -> false
 )

Another way to do it is to define a custom form field. See this link

@helper.form(action = routes.Application.submit(), 'id -> "myForm") {
    <select>
        <option>This is option 1</option>
        <option>This is option 2</option>
        <option>This is option 3</option>
    </select>
}

Please be sure to do an extensive Google search before you ask these questions. I am sure there are tutorials and or the same question that has been already been asked

Cheers

like image 113
Omar Wagih Avatar answered Mar 16 '26 00:03

Omar Wagih