简体   繁体   English

玩! 框架ENUM和Groovy问题

[英]play! framework ENUM and Groovy problem

I have something like the following- 我有类似以下的东西 -

Woman.java Woman.java

...
@Entity
public class Woman extends Model {

    public static enum Outcome {
        ALIVE, DEAD, STILL_BIRTH, LIVE_BIRTH, REGISTER
    }
    ...
}

File.java File.java

...
@Entity
public class Form extends Model {
    ...
    public Outcome autoCreateEvent;
    ...
}

Create.html create.html上

#{select "autoCreateEvent", items:models.Woman.Outcome.values(), id:'autoCreateEvent' /}

It saves ENUM value in DB, which is OK. 它将ENUM值保存在DB中,这是可以的。 But, when I reload/edit then the problem rises. 但是,当我重新加载/编辑时,问题就出现了。 Because it uses ALIVE, DEAD, etc. as the value for options so it can't show the list properly. 因为它使用ALIVE,DEAD等作为选项的值,所以它无法正确显示列表。

Any Insight? 任何洞察力?

If I understand your question properly you want to use the valueProperty and labelProperty to set the proper values in the option . 如果我正确理解您的问题,您想使用valuePropertylabelPropertyoption设置正确的值。 Something like: 就像是:

#{select "autoCreateEvent", items:models.Woman.Outcome.values(), valueProperty:'ordinal', labelProperty: 'name', id:'autoCreateEvent' /}

EDIT: 编辑:

For this to work you will need to tweak the enum a bit, like this: 要实现这一点,您需要稍微调整枚举,如下所示:

public enum Outcome {
  A,B;

  public int getOrdinal() {
     return ordinal();
  }

}

The reason is that Play #{select} expects getters in the valueProperty and labelProperty params, and when not found defaults to the enum toString 原因是Play#{select}期望在valuePropertylabelProperty params中valueProperty getter,并且在找不到时默认为enum toString

To add to previous answer, add this to your Enum declaration: 要添加到上一个答案,请将其添加到您的Enum声明中:

public String getLabel() {
    return play.i18n.Messages.get(name());
}

Make sure to use the following declaration: 请务必使用以下声明:

#{select "[field]", items:models.[Enum].values(), valueProperty:'name', labelProperty: 'label' /}

You can also add this in the Enum: 您也可以在枚举中添加:

    @Override
public String toString() {
    return getLabel();
}

Which will be useful if you want to display the internationalized value in your view file (since toString is being called automatically when displayed) but function name() uses toString() so you will have to bind valueProperty to another function, as follow: 如果要在视图文件中显示国际化值,这将非常有用(因为在显示时会自动调用toString),但函数name()使用toString(),因此您必须将valueProperty绑定到另一个函数,如下所示:

public String getLabel(){
    return toString();
}

public String getKey() {
    return super.toString();
}

@Override
public String toString() {
    return Messages.get(name());
}

And the #select use: 并且#select使用:

#{select "[field]", items:models.[Enum].values(), value:flash.[field], valueProperty:'key', labelProperty: 'label' /}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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