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'/>
Related
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.
please help me this issues
I got this select box
<select name="company">
<g:each in="${grailsApplication.config.Companies['No']}" var="no" status="index">
<option value="${no}">${no}: ${grailsApplication.config.Companies['Name'][index]}</option>
</g:each>
</select>
the select box has value like this
option1 01: abc
option2 02:def
Then, I used form update, when I choose option2, it saves value to db, but on select box, default value is 01:abc, how can i change it to 02:def after update.
Assuming that you are sending saved value in savedValue:
<select name="company">
<g:each in="${grailsApplication.config.company}" var="company" status="index">
<g:if test="${grailsApplication.config.company['no'][index] == savedValue}">
<option value="${grailsApplication.config.company['no'][index]}" selected>${grailsApplication.config.company['name'][index]}</option>
</g:if>
<g:else>
<option value="${grailsApplication.config.company['no'][index]}">${grailsApplication.config.company['name'][index]}</option>
</g:else>
</g:each>
</select>
How would I go about setting up a I18N message properties file with all the US states. I want to be able to add these to a g:select tag in my gsp file.
I'm spinning my head seeing different ways online to do this. And being new to grails isn't helping. Any help appreciated.
Example:
<select>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
.......
</select>
I see a couple ways to do this....
In Config.groovy:
com.app.states=[[abbr: 'AL', name: 'Alabama'],[abbr: 'AK', name: 'Alaska']..........]
Then in your gsp:
<g:select name="state" from="${grailsApplication.config.com.app.states}" optionKey="abbr" optionValue="name" noSelection="${['null':'Select One...']}" value="${obj.state}"/>
Another way
Make an enum for your abbreviations:
package com.app.enum
public enum State {
AL, AK, .........., WY
}
Add messages in messages.properties:
com.app.enum.AL=Alabama
com.app.enum.AK=Alaska
...
...
...
Then in your GSP:
<g:select name="state" from="${com.app.enum.State.values()}" optionKey="key" valueMessagePrefix="com.app.enum" noSelection="${['null':'Select One...']}" value="${obj.state.key}"/>
I kinda preffer the second way. I would also type the state field on my model as the State enum type instead of just a String. But either of these should work for you...
I would like to make a select box using <g:select/> that translates to this html:
<select id="myselect" name="myselect">
<option value="r">RED</option>
<option value="g">GREEN</option>
<option value="b">BLUE</option>
</select>
I would also like the value to be preselected from a bean when the page reloads.
I'm doing this inside a so I have a table with each row having a separate option box.
I'm currently accomplishing this in the below html:
<g:each in=${mylist} status="i" var="myInst">
<select id="status${myInst}" name="status${myInst}" data-id="${myInst.id}">
<option value="r" <g:if test="${myInst.color == "r"}">selected</g:if>>RED</option>
<option value="g" <g:if test="${myInst.color == "g"}">selected</g:if>>Green</option>
<option value="b" <g:if test="${myInst.color == "b"}">selected</g:if>>BLUE</option>
</select>
</g:each>
This all works fine but I'd like to change that ugly <select> into <g:select>
<g:select id="myselect" name="myselect" value="${myInst.color}"
from="${['r': 'RED', 'g': 'GREEN', 'b': 'BLUE']}"
optionKey="key" optionValue="value" />
you have to declare the "myselect" inside your domain class. I have been having trouble with this too, but I'm about 2 weeks ahead of you. see how do I write a set for 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"