玩!框架ENUM和Groovy问题



我有如下内容-

Woman.java

...
@Entity
public class Woman extends Model {
    public static enum Outcome {
        ALIVE, DEAD, STILL_BIRTH, LIVE_BIRTH, REGISTER
    }
    ...
}

File.java

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

Create.html

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

将ENUM值保存在DB中,这是OK的。但是,当我重新加载/编辑时,问题就出现了。因为它使用ALIVE, DEAD等作为选项的值,所以它不能正确显示列表

见解吗?

如果我正确理解你的问题,你想使用valuePropertylabelPropertyoption中设置适当的值。比如:

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

要使其工作,您需要稍微调整enum,如下所示:

public enum Outcome {
  A,B;
  public int getOrdinal() {
     return ordinal();
  }
}

原因是Play #{select}期望在valuePropertylabelProperty参数中获得getter,当没有找到时默认为enum toString

要添加到前面的答案,请将此添加到Enum声明中:

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

确保使用以下声明:

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

也可以在Enum中添加:

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

如果你想在视图文件中显示国际化的值,这将是有用的(因为toString在显示时被自动调用),但是函数名()使用toString(),所以你必须将valueProperty绑定到另一个函数,如下所示:

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

和#select use:

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

最新更新