1

我有两个域类:-

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()
  }
}

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

4

1 回答 1

1

您可以在您的 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}" />
于 2012-05-22T07:14:33.980 回答