Grails multiple g:if test for more than one condition - grails

I have a select box of TYPES that each type has their own PARAMETERS. The TYPES select box will fire off some AJAX that calls a template and renders PARAMETER select boxes on my view. The Parameters are made up of name:value pairs, thus every name can have many values.
Some parameters need multiple='true' for the user to select multiple values for each name, while other parameters need to be restricted to only one choice.
In my gsp page I have a bunch of these:
<g:if test="${it?.getKey().toString().equals('PARAMETER_A')}">
<td><g:select multiple="true" optionKey="id" optionValue="value" name="sampleParameters" id="parameter" value="${params?.sampleParameters}" from='${it?.getValue().sort()}'></g:select></td>
</g:if>
<g:if test="${it?.getKey().toString().equals('PARAMETER_B')}">
<td><g:select multiple="true" optionKey="id" optionValue="value" name="sampleParameters" id="parameter" value="${params?.sampleParameters}" from='${it?.getValue().sort()}'></g:select></td>
</g:if>
My issue is that I have 6 parameters for one particular TYPE that need to select multiple values, the rest do not. Rather than explicitly type out as above, is there a way that I can test for more than one thing in a g:if statement like you can in java? such as:
if(something.equals(PARAMETER_A) || something.equals(PARAMETER_B))
etc.
Is there a way to do something similar to java's approach in groovy?

Grails g:if just uses groovy in its test attribute. So to answer your question, yes:
<g:if test="${something.equals(PARAMETER_A) || something.equals(PARAMETER_B)}">
</g:if>

Related

Struts2 use <s:select> with <s:iterator>

Is it possible to define a Struts Select field, <s:select>, with an Iterator for options, <s:iterator>?
e.g., I don't want to use the Key/Value/List properties,
<s:select id="criteriaRequestStatusList" name="searchRequestCriteria.requestStatusList"
list="requestStatuses"
listValue="description" listKey="id" />
because I have some special symbols such as coming from the server side and they aren't escaped.
The following works regarding escaping , but it's an HTML Select. I don't want to use this either, because it doesn't populate the form values properly on load.
<select id="criteriaRequestStatusList" name="searchRequestCriteria.requestStatusList" class="requestor input-block-level" name="requestors" multiple="multiple">
<s:iterator value="requestStatuses">
<option value="${id}">${description}</option>
</s:iterator>
</select>
My goal is,
<s:select ..>
<s:iterator>
You can set the value on the POHTML select using the normal value attribute.
The <s:select> tag expects values to be ready-to-use; personally I'd transform them on the server side before exposing them to the view layer.
You might be able to use OGNL in the listTitle property (I might have the property name wrong) to un-entity it; I don't recall.

Select dropdown with f:field (fields plugin) in Grails 3

I'm trying to generate a select drop down with an option preselected (based on params) using the fields plugin. The code below generates a select box with the proper elements and everything works except it doesn't add "selected" to the option tag. It seems to be ignoring my widget- attributes.
From what I understand the fields plugin will pick g:select in this case by default if not overridden, and you can use widget- to pass arguments to it.
What am I doing wrong?
<f:field bean="specialUser" property="user" label="user.label" widget-optionKey="id" widget-value="${params.user?.id?:value?.id}"/>
Link to the fields documentation:http://grails3-plugins.github.io/fields/snapshot/guide/single.html#
This seems to have done what I wanted, computed specialUserList from the controller. Though it would've been nice to utilize the "widget-" to do this form me.
<f:field bean="specialUser" property="user" label="user.label">
<g:select id="user" name="user.id" optionKey="id" class="span-8 margin-bottom-none" from="${specialUserList}" noSelection="${['null': message(code: 'common.list.select')]}" value="${params.user?.id}"/>
</f:field>

g:select tag in grails does not store key of list element in target field

I have the following codeline in my grails view
<g:select id="partner" name="partner.id" from="${org.strotmann.partner.Partner.partners}" optionKey="id" value="${auftragInstance?.kundenNr}" class="many-to-one" noSelection="['null': '']"/>
I expect it to show me the list specified in the from clause as a select box (that works fine)
After selecting an Item from the box I expect that it stores the id of that Item in the field specified in the value clause ( that does not work, always null)
I'm somewhat confused, because the followin codeLine does exactly what I expect:
<g:select id="partner" name="partner.id" from="${org.strotmann.partner.Partner.partners}" optionKey="id" value="${arbeitsgangInstance?.kundenNr}" class="many-to-one" noSelection="['null': '']"/>
Can anybody tell me where I can find a difference or how to recode my ?
peter
let me reformulate my question:
I have a list
org.strotmann.partner.Partner.partners
It contains partners with just the attribute name and of course the id.
I want the names in a selectList and want the id of a partner stored in
auftragInstance.kundenNr
feel free to code a working g:select
peter
The value attribute is used to provide the initial selection. The name attribute will contain the selected value when the form is submitted. In a controller you can get it like this:
def partner = params.partner
See:
select tag in grails documentation
thanks to comment from masc I found that there was just missing the following code snippet in save and update method of AuftragController:
if (params.partner.id == 'null')
salesOrderInstance.kundenNr = 0
else
salesOrderInstance.kundenNr = params.partner.id.toLong()
That code block was allready contained in ArbeitsgangController, and therefore the second html-line in my question was working fine.
the following select (with id and value clauses omitted) will also do:
<g:select name="partner.id" from="${org.strotmann.partner.Partner.partners}" optionKey="id" class="many-to-one" noSelection="['null': '']"/>
sorry the value entry
value="${auftragInstance?.kundenNr}"
must not be removed, it is important for update.

How to restrict the visible size of select box in Grails to 1?

With the <g:select> tag... sometimes it displays normally like a selection drop down box, while sometimes it displays with multiple rows, this is very annoying.... Even I put the size="1" into the <g:select>, it still displays multiple rows... is there anyone knows how to make <g:select> display correctly? with only one item visible, like a dropdown box. Thanks!!
<g:select size="1" id="s_caseID" name="s_caseID" value="${t1T2InstanceListTotal?.Accession_No}"
noSelection="${['null':'Select One...']}"
from='${t1T2InstanceListTotal}'
optionKey="Accession_No" optionValue="Accession_No" onclick="this.form.submit();" >
</g:select>
Here's the taglib code that cause the multiple="multiple" attribute to be rendered (if not explicitly declared on the tag):
def value = attrs.remove('value')
if (value instanceof Collection && attrs.multiple == null) {
attrs.multiple = 'multiple'
}
Therefore, it looks like you're passing a Collection as the <g:select>'s value attribute instead of a single value. Is that what you're intending to do?
Set the multiple attribute to false
<g:select name="cars"
from="${Car.list()}"
value="${person?.cars*.id}"
optionKey="id"
multiple="false" />
If the "value" is a list, g:select always considers it as a multiple select. To avoid this and have a single select drop-down just ignore the value attribute and use keys option instead!
This works fine for me!
`<g:select id="s_caseID" name="s_caseID" from='${t1T2InstanceListTotal}'
noSelection="${['null':'Select One...']}"
keys="${t1T2InstanceListTotal?.Accession_No}" onclick="this.form.submit();">
</g:select>`

Why use <g:textField /> in Grails?

What is the reason to use g:textField in Grails if you're already familiar with standard HTML form tags?
If I understand correctly the following two markup alternatives are equivalent:
<input type="text" name="name" value="${params.name}" id="name" />
<g:textField name="name" value="${params.name}" />
Are there any circumstances under which using g:textField would add value? Am I missing something?
The textField tag is provided as a convenience (slightly shorter than writing the HTML input) and is there to provide a full set of form tags. Personally I prefer to write as much plain HTML as possible in my Grails views and only tend to use the tags that really offer a benefit such as the form tag. I have not found an instance where using textField would have added any value outside of requiring a few less characters to type.
<g:textField /> is not shorter as plain text field tag, except id attribute will be attached automatically.
However, I recommend you to use customized tags associating the bean values with input fields. That shortens the code a lot. For more information you can read http://www.jtict.com/blog/shorter-grails-textfield/
Also you can find useful stuff in Form Helper Plugin

Resources