How to use g:render in g:select for optionValue? - grails

I have a g:select statement that looks like the following:
<g:select id="gearbox" name="gearbox.id" from="${com.nppc.mes.energyusage.Gearbox.list()}" optionKey="id" optionValue="${ {"${it.gearboxType} - (${it.gearboxRatio})"} }" required="" value="${gearboxVoltageInstance?.gearbox?.id}" class="many-to-one"/>
I have added an optionValue attribute: optionValue="${ {"${it.gearboxType} - (${it.gearboxRatio})"} }"
This works as I want.
However, I want to show my domain object, Gearboxes the same everywhere. I have created a template, and am able to use the g:render tag on my show.gsp.
What I can't figure out is how to get something like this to work:
optionValue="<g:render template="/shared/gearbox" model:="[gearbox:it]"/>"
Is it possible to use templates to generate the content that goes into an optionValue?

You could hack around it, but it feels like bad design to me.
The right way to do something like is seems to be to add a helper method to your Gearbox domain class
static transients = [ 'renderedValue' ]
def getRenderedValue(){
"$gearboxType - $gearboxRatio"
}
Then, you can just call optionValue="${ it.renderedValue }".
Also, if you specify a toString() in your domain class, this would be used to generate the default rendered value.

Tags can be used as regular functions in Groovy code. Like:
optionValue="${g.render(template: '/shared/gearbox', model: [gearbox: it])}"

Related

Use Grails Form tag with Controller in Subfolder

I'd like to send an email using a controller in a subfolder. I can't seem to find the syntax to accomplish this.
The default would be something like this...
<g:form controller="email" action="sendEmail">
// form stuff
</g:form>
But my email controller, called Email.groovy, is in controllers/portal/Utils. Maybe this is outside normal convention?
Change your controller class name to EmailController.groovy

Use placeholders with <f:all bean />

In Grails, you can generate a form using <f:all bean="beanName" />. And while the generated form looks great, I was wondering if it would be possible to have a placeholder in the field, so that the result becomes something like:
<input type="text" name="question" placeholder="type your question here" />
I tried using the attributes validation in Grails, like this:
class Question {
static constraints = {
question(size:5..100, attributes:[placeholder:"type your question here"])
}
}
But it doesn't seem to have any effect on the generated HTML.
Just in case there's any confusion, the f:all tag is provided by the fields plugin. I don't think there's any way you can specify the placeholder attribute via the domain class constraints, but there are a few other options.
One option is to define a custom (GSP) template for this property and specify the placeholder attribute therein. The path to this template will depend on which version of the plugin you're using, but you can find the details here.
Alternatively, if you render each field individually with f:field, rather than using f:all you can pass additional attributes to the input field, e.g.
<f:field bean="person" property="gender"
widget-placeholder="type your question here"/>
In earlier versions of the plugin (before 1.5), the attribute should be named input-placeholder instead, e.g.
<f:field bean="person" property="gender"
input-placeholder="type your question here"/>

In Tritium, how do I add an attribute with no value assigned to it?

I have a normal tag:
...
I need it to look like this:
<a href="prefetchThisPage.html" data-prefetch> ... </a>
Why? jQuery Mobile feature: http://jquerymobile.com/demos/1.1.1/docs/pages/page-cache.html
The expected way below does not add the attribute, probably because Tritium ignores empty attributes.
attribute("data-prefetch")
Any ideas?
Great Question!
The best way to do this, is to set the value of the variable to the NAME of the variable.
I would do it like this:
$variable_name = "data-prefetch"
$("./a") {
attribute($variable_name, $variable_name)
}
This is an HTML5 convention and should work for the purposes you illustrated!
Tritium currently has a short circuit that converts attribute assignments like the following declaration:
attribute("data-prefetch", "")
Into the equivalent of removing the attribute.
I strongly recommend you file this issue with Moovweb's help desk so that they add support for adding attributes with no values.

Access the inList-values in View [Grails]

i want to access the inList-values which i set in domain, in my view. Like this:
<g:countrySelect name="SelectState" keys="${incidentInstance?.state.toList()}" value="${incidentInstance?.state}"/>
But it dont work...(any idea?)
Thx
Well, you are using <g:countrySelect> instead of <g:select>
To get the inList values (from your constraints definition) you do something like this
<g:select name="state" from="${incidentInstance.constraints.state.inList}"
value="${incidentInstance?.state}"/>

GSP include - with parameters -within g:each?

I have some common structure that I'd like to use, and I need to nest it within a <g:each />. The semantic solution seems to be to create a taglib, but I'd rather write GSP code to construct the structure, as it's easier to read/maintain.
Essentially, I need this:
<g:each ...>
//Somehow include a GSP that takes parameter "datastructure1"
//Again with "datastructure2"
</g:each>
A GSP include takes params, but it's not nestable in a <g:each />. What am I looking for?
<g:include> works as you need it to. Try again.
You can call a taglib, then in the taglib function: render a template. There's an example here.

Resources