如何在Grails 4中的创建和编辑表单中添加多个枚举字段? [英] How to add a multiple enum field to the create and edit forms in Grails 4?

查看:52
本文介绍了如何在Grails 4中的创建和编辑表单中添加多个枚举字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的域模型中,某些类将枚举的集合作为字段.我以两种不同的方式对其进行了建模,作为嵌入式集合:

In my domain model, some classes have a collection of enums as a field. I have modeled it in two differente ways, as an embedded collection:

class A {
  String name
  Set<Enumeration> enumerations

  static embedded = ['enumerations']
}

enum Enumeration {
  ENUM_VALUE_1,
  ENUM_VALUE_2,
  ENUM_VALUE_3
}

以及作为 hasMany 集合:

class A {
  String name

  static hasMany = [enumerations:Enumeration]
}

enum Enumeration {
  ENUM_VALUE_1,
  ENUM_VALUE_2,
  ENUM_VALUE_3
}

在两种情况下,我都可以通过以下方式将枚举值添加到 BootStrap.groovy 中的集合中:

In both cases, I can add enum values to the collection in BootStrap.groovy in the following way:

A.withTransaction { status ->
  def a1 = new A( name:"a1" )
  a1.addToEnumerations( Enumeration.ENUM_VALUE_1 )
  a1.addToEnumerations( Enumeration.ENUM_VALUE_2 )
}

使用脚手架,我可以在 index show 页面中看到枚举集合的内容,但是在 edit 创建页面,仅显示标签,不显示小部件.

Using scaffolding, I can see the content of the enumeration collection in the index and show pages, but in the edit and create pages, only the label is shown, no widget is displayed.

哪种是显示窗口小部件的最简单方法,例如是针对Grails 4中的此类字段(我正在使用Grails 4.0.3)进行多次选择吗?

Which is the simplest way to show a widget, e.g. a multiple select, for this kind of fields in Grails 4 (I am using Grails 4.0.3)?

谢谢.

推荐答案

哪种是显示窗口小部件的最简单方法,例如多项选择在Grails 4中使用这种字段(我正在使用Grails 4.0.3)?

Which is the simplest way to show a widget, e.g. a multiple select, for this kind of fields in Grails 4 (I am using Grails 4.0.3)?

您可以使用 select 标记.

<g:select multiple="true" name="someName" from="${somePackage.SomeEnumClassName}"/>

这篇关于如何在Grails 4中的创建和编辑表单中添加多个枚举字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆