2012-10-09 70 views
2

我使用播放框架2.0.4填充@select下拉列表中的Java

在我的Java文件,

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


在我的HTML文件,

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

我想通過在@import helper中使用@select從名稱數組中創建一個下拉列表(如使用select,選項標記在純HTML中)._
我對Play很新穎,因此有人可以告訴我如何將其存檔?
非常感謝。

+0

檢查同樣的問題https://github.com/playframework/Play20/tree/master/samples/java/computer - 數據庫/ - 在'editForm'視圖和'計算機'模型中檢查如何準備和使用選項('Map '),最好的辦法是使用List – biesior

+0

然後BTW我仍然沒有理解你的問題,我希望這個建議能幫助你找到你需要的東西 – biesior

回答

8

一種方式來做到這一點是通過定義選項列表,通過一個靜態方法

返回創建一個Java類

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; 
    } 
.... 
} 

在HTML中,導入助手

@import helper._ 

並嘗試

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

另一種方法是定義一個自定義表單域。看到這個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> 
} 

請確保做一個廣泛的谷歌搜索之前,你問這些問題。我肯定有教程和或已經已經問

乾杯

4
Use String in List[String] (in your html) List<String> in your java file. 
Or if you want both value and text of drop down to be different like : 

    <option value="1">One</option> 

Use Map<String, String> instead of List<String> and pass it to @select 

    Java file: 
    Map<String, String> options = new HashMap<String, String>(); 
     options.put("1", "One"); 
     options.put("2", "Two"); 
    return ok(views.html.name.render(options, NameForm)); 

    Html: 
    @(name: Map<String, String>,NameForm: Form[Name])