Reputation: 1936
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.
Upvotes: 0
Views: 563
Reputation: 528
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}}"
Upvotes: 2
Reputation: 240860
override toString()
in class ProductName
to return just productName
like
class ProductName
{
String name
@Override
public String toString(){ return name }
}
Upvotes: 2