Change the value of an <select> on grails - grails

I have the <g:select> on my proyect who is a one-to-many relation,
but I want it to show the name (attribute) of my class instead of the id.
It shows something like this:
com.petshop.Category: 1
This is the code I use on the view:
<g:select id="subcategory" name="subcategory.id" from="${com.petshop.Category.list()}" optionKey="id"
required="" value="${animalInstance?.category?.id}"/>
What should I change/add/delete to show "Birs" for instance, instead of "com.petshop.Category: 1" that is the id of the object.

Would this work?
<g:select id="subcategory" name="subcategory.name" from="${com.petshop.Category.list()*.name}" optionKey="id" optionValue="name"
required="" value="${animalInstance?.category?.name}"/>

Related

How to create a dropdown with 'option group' on Grails 4?

I wanna create a dropdown with option group. Controller return a list which is options items and get the list on GSP page. I need to populated that option item with option group.
<select class="" name="name" id="name">
<g:each in="${list}" var="opt">
</g:each>
</select>
Anybody can give an example with backend code. Thanks
You should use the <g:select ...> tag.
<g:select optionKey="id" optionValue="title"
name="book.title" from="${bookList}" />
See https://gsp.grails.org/latest/ref/Tags/select.html.

Add HTML "required" in Grails Select Tag

I am trying to add a select dropdown button option using the Grails select tag. I need to make this field required and I am unable to do so.
This is what I have so far.
<g:select class="form-control" name:"test" from=${[test1:"Test 1", test2: "Test 2"]} optionKey="key" optionValue="value" />
Addrequired="required" as an attribute. Most Grails tags will simply include additional attributes as HTML attributes.
For example:
<g:select class="form-control" name:"test" from=${[test1:"Test 1", test2: "Test 2"]} optionKey="key" optionValue="value" required="required" />
Will render a <select> element with the required="required" attribute on it. Depending on how you are validating the form that may be enough.
grails select doesn't offer required prop. But you can work around by using Jquery validator.

Grails g:select optionValue

I having a grails element
<g:select name="name" from="${list}" optionKey='code' optionValue='name' ></g:select>
where the optionValue contains some HTML elements like this,
I want to show only the country name, already I tried using encodeAsHTML(), but no idea how to use. Please suggest.
Thanks
you can not do this with the out-of-box g.select tag. you need to iterate through your list manually:
<select name="someName">
<option value="">- no select -</option>
<g:each in="${list}" var="c">
<option value="${c.code}">${c.name.replaceFirst( /<span class='countryName'>([\w\s]+)</span>/, '$1' )}</option>
</g:each>
</select>
This is happening because of XSS. Instead of encodeAsHTML use raw. Try this:
<g:select name="name" from="${list.collect { raw(it) }}" optionKey='code' optionValue='name'/>

Retrieving children from a particular parent for display on a select component

I have a parent-child domain-class relation in Grails as shown, and I want to retrieve all the children of one specific parent object in order to display them in a HTML select component for the user to pick.
class Parent {
static hasMany = [children: child]
}
class child {
string name
Parent parent
static belongsTo = [Parent]
}
I know I should use a Grails' select tag of the form:
<g:select name="user.company.id"
from="${parent}"
value="${user?.company.id}"
optionKey="id" />
I tried doing this, but it does not seem to work:
<g:select name="child.id"
from="${the parent.children}"
optionKey="id" />
Any suggestions on what the correct syntax is? Thanks
Try this for a drop-down:
<g:select name="children"
from="${parent.children}"
optionKey="id" optionValue="name"/>
For multiple selects you can use:
<g:select name="children"
from="${parent.children}"
value="${parent.children.id}"
optionKey="id" optionValue="name"
multiple="true"/>

GSP select tag doesnt work with findAll()

I want to show select box in GSP with some values from DB table.
Following code works fine
<% List a = test.demo.MyCategory.findAll("from MyCategory where is_deleted = false"); %> &ltg:select name="myCategory.id" from="${a}" optionKey="id" optionValue="name" />
But when I try this by writing query directly in tag, it doesn't work.
<g:select name="myCategory.id" from="${test.demo.RecipeCategory.findAll("from MyCategory where is_deleted = false")}" optionKey="id" optionValue="name" />
I think the problem are the double quotation marks in the query string.
I got the answer
<g:select name="myCategory.id" from="${test.demo.MyCategory.findAllWhere(isDeleted:false)}" optionKey="id" optionValue="name" />

Resources