【问题标题】:How to prefill a dropdown using scala template and play framework如何使用 scala 模板和播放框架预填充下拉列表
【发布时间】:2015-05-11 20:27:00
【问题描述】:

我正在为我的项目使用 scala 模板和 Play 2.0 框架。假设我有一个用户表单,其中包含名称(文本字段)、年龄(下拉菜单)等字段。在创建用户时,我将姓名填写为 dave,并将年龄选择为 25。

现在在我的编辑屏幕上,我希望预先填写我的值,我知道如何使用文本字段(即将值设置为 userForm('name'))但下拉列表呢?怎么做。

【问题讨论】:

  • 我认为这已经得到了回答:stackoverflow.com/questions/12798174/…
  • 没有@shawn,我刚刚浏览了链接,它告诉了如何使用 scala 模板助手创建下拉列表,但不设置预填充值。
  • 你在说什么样的下拉菜单? HTML 的 select 标签或 Bootstrap 的下拉菜单之类的东西?对于select 标记,建议的链接是有效的...描述您所说的 prefilled 是什么意思?
  • 好吧,我正在使用 @select (这是一个 scala 助手)并且预填充的意思是,如果用户输入他的年龄为 25 岁(通过下拉菜单),那么在编辑屏幕上他的年龄应该是页面加载时显示为 25。提前谢谢你。

标签: scala playframework scala-template


【解决方案1】:

感谢 Shawn Downs 和 biesior。

好吧,我们可以使用@select scala 辅助类来显示预填充的结果。 喜欢。

 @select(userForm("age"),models.Age.values().toList.map(v => (v.getDisplayName(), v.getDisplayName())),'id->"age")

为了显示其他选项,我使用了年龄可能值的枚举。

【讨论】:

    【解决方案2】:

    在你的模型中会有 2 个字段

    型号代码

    Class User{
       public String name;
       public int age;
    }
    

    控制器代码

    .
    .
    .
    Form<User> userForm = Form.form(User.class);
    User user = new User();
    user.name = "Albert";
    user.age = 19;
    
    userForm.fill(user);
    .
    .
    .
    

    实用代码

    package utils;
    
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    
    public class DropdownUtils {
    
        public static HashMap<String, String> getAgeList(int ageLimit){
            LinkedHashMap<String, String> ageList = new LinkedHashMap<>();
    
            for(Integer i=0; i<=ageLimit; i++){
                ageList.put(i.toString(), i.toString());
            }
    
            return ageList;
        }
    }
    

    查看代码

    .
    .
    .
    <form>
    @helper.inputText(userForm("name"))
    @helper.select(userForm("age"),
                    helper.options(utils.DropdownUtils.getAgeList(25)))
    </form>
    .
    .
    .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 2015-07-31
      • 2013-05-10
      相关资源
      最近更新 更多