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

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.

Related

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'/>

Rails - Submit Values based on Multiple Selection and Text Field

I'm still new in Rails. I'm trying to have a page where a user can select multiple items from a list then add some description into a text field. And at the end, the user should submit this information. How can I retrieve submitted data?
Any help would be greatly appreciated.
Thanks!
Here's the code for now:
<select multiple >
<option>Table</option>
<option>Door</option>
</select>
<br><br><br>
<select multiple >
<option>Blue</option>
<option>Red</option>
</select>
<input type="text" id="post_user" name="post" size="20" value="" />
All the form (POST) data can be found in the params hash. See attached links for details:
http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters
Rails params explained?

Change the value of an <select> on 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}"/>

how to check for a condition in g select tag

I'm a newbie to Grails and GSPs.
I need to achieve the following code using g:select tag
<select name="applaiances" style="width: 200px" onchange="selectedTC(this); ">
<g:each in="${applaianceList}" var="appl">
<g:if test="${appl == "TELEVISION"}">
<option value="TELEVISION">TV</option>
</g:if>
<g:else>
<option value="${appl}">${appl}</option>
</g:else>
</g:each>
</select>
Any help would be appreciated.
Haven't tried this in an app but you could try something like:
<g:select name="applaiances" onchange="selectedTC(this);" from="${applaianceList}" optionKey="${{it=='TELEVISION'?'TELEVISION':it}}" optionValue="${{it=='TELEVISION'?'TV':it}}"></g:select>
Not sure about the optionKey but you can apply transformation via a closure on the optionValue.
Update:
This is documented here. Just search for the phrase "If you require even more control over how each"

Resources