grails - 域内域

标签 grails groovy

我有两个Domain类:

class Product {
  ProductType productType
  int openingQuantity
  int unitQuantity
  Date dateCreated
  Date lastUpdated
  boolean active
  static constraints = {
    productType(blank:false,nullable:false)
    openingQuantity(blank:false, nullable:false)
    unitQuantity(blank:false, nullable:false)
    active(nullable:false)
    dateCreated()
    lastUpdated()
  }
}


class ProductType {
  String name
  Date dateCreated
  Date lastUpdated

  static constraints = {
    name(nullable:false, blank:false,maxSize:50,validator: {
      return !ProductType.findByNameIlike(it)
    })
    dateCreated()
    lastUpdated()
  }
}

当我使用Product.gsp产品时。它在下拉列表中将productType显示为id。但我的要求是在下拉列表中显示ProductType名称。谁能帮忙。

最佳答案

您可以在ProductType类上重写toString。

String toString() { name }

或者,假设您使用的是默认脚手架,请更改:
<g:select name="productType.id"
          from="${com.ten.hp.his.pharmacy.ProductType.list()}"
          optionKey="id"
          value="${productInstance?.productType?.id}" />

通过添加optionValue,它看起来像:
<g:select name="productType.id"
          from="${com.ten.hp.his.pharmacy.ProductType.list()}"
          optionKey="id"
          optionValue="name"
          value="${productInstance?.productType?.id}" />

关于grails - 域内域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10697088/

相关文章:

jenkins - 从 jenkins groovy 调用 windows sc 命令时提示访问被拒绝

date - 字符串到最新的groovy

gradle - Gradle 中 "def"和 "static def"之间的区别

grails - 使用 grails 动态查找器时出现 org.springframework.dao.InvalidDataAccessApiUsageException

java - Grails - 项目构建系统

java - 使用 GroovyFX 实现新的 JavaFX 组件?

grails - Grails Controller 和继承/关闭

java - 退出 java (Grails) 应用程序后, "used"内存未释放

javascript - 在服务器端Groovy页面中访问客户端jquery变量

hibernate - 在GORM中使用复合键