玩!框架ENUM和Groovy问题

发布于 2024-11-28 05:36:47 字数 639 浏览 6 评论 0原文

我有类似以下内容的内容 -

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 值保存在数据库中,这是可以的。但是,当我重新加载/编辑时,问题就会出现。因为它使用 ALIVE、DEAD 等作为选项的值,所以它无法正确显示列表。

有什么见解吗?

I have something like the following-

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' /}

It saves ENUM value in DB, which is OK. 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.

Any Insight?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半﹌身腐败 2024-12-05 05:36:48

如果我正确理解您的问题,您希望使用 valuePropertylabelPropertyoption 中设置正确的值。像这样的东西:

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

编辑:

为此,您需要稍微调整枚举,如下所示:

public enum Outcome {
  A,B;

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

}

原因是 Play #{select} 需要 valuePropertylabelProperty 中的 getters code> params,未找到时默认为枚举 toString

If I understand your question properly you want to use the valueProperty and labelProperty to set the proper values in the option. 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

软甜啾 2024-12-05 05:36:48

要添加到前面的答案,请将其添加到您的枚举声明中:

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

确保使用以下声明:

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

您还可以在枚举中添加它:

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

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

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

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

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

#select 使用:

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

To add to previous answer, add this to your Enum declaration:

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:

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

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

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

And the #select use:

#{select "[field]", items:models.[Enum].values(), value:flash.[field], valueProperty:'key', labelProperty: 'label' /}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文