简体   繁体   中英

how to displaydomain value in gsp

I have three domain class

class Stock
{
   Product product
}

and

class Product
{
   ProductName productName
}

and

class ProductName
{
   String name
}

In create.gsp of Stock domain, this is the default code generation:-

<tr class="prop">
  <td valign="top" class="name">
    <label for="name">
      <g:message code="stock.name.label" default="Product Name" />
    </label>
  </td>
  <td valign="top" class="value ${hasErrors(bean: stockInstance, field: 'name', 'errors')}">
    <g:select name="product.id"
              from="${com.ten.hp.his.pharmacy.Product.list()}"
              optionKey="id"
              optionValue="productName"
              value="${stockInstance?.product?.id}"  />
  </td>
</tr>

my requirement is to display the product name in the drop down but by using optionValue, it is showing productName id like, com.ten.ProductName:1 . how can i show, product name in the drop down.

override toString() in class ProductName to return just productName

like

class ProductName
{
   String name

   @Override
   public String toString(){ return name } 
}

Jigar answer is correct based on your data model. The grails select tag expects the optionValue to be the name of the bean property of your element (Product). The select tag will call toString() on that value so to work with this you need to override the toString() method of the ProductName class.

It is also possible to specify within the gsp that you want the "name" property of the ProductName by passing a closure code as the optionValue.

optionValue="${{it.productName.name}}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM