Using Struts2, I have a very simple radio tag like following
<s:radio label="correctOption" name="correctAnswer" list="
#{'1':'1','2':'2','3':'3','4':'4'}" value="questionVo.correctAnswer"/>
questionVo.correctAnswer returns 2. So I want the second radio button to be preselected but it is not happening. I even tried:
<s:radio label="correctOption" name="correctAnswer" list="
#{'1':'1','2':'2','3':'3','4':'4'}" value="%{1}"/>
But that does not work either.
What am I doing wrong?
Remove the value attribute from the jsp. Then in your Java code make sure that the "correctAnswer" variable has the value you want.
This has also the added effect that works in postbacks (when the user has selected something
on the radio and the form is shown again)
It works for me for the following:
<s:radio
label="correctOption"
name="correctAnswer"
list="#{'1':'1','2':'2','3':'3','4':'4'}"
value="%{1}"/>
I believe that the issue is that the value needs to be escaped properly eg:
value="%{'1'}"
and match the declaration exactly.
Here's the solution:
<s:radio label="correctOption" name="correctAnswer" list="
#{'1':'1','2':'2','3':'3','4':'4'}" value="1"/>
another one in case of strings in the static list:
<s:radio label="Gender" name="gender" list="
#{'male':'Male','female':'Female'}" value="'male'"/>
Related
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>
I am developing an application using Struts2 and jquery and would like to set maxlength for struts2 s:textarea tag. Unlike Struts2 s:textfield, the s:textarea tag does not seem to be supporting maxlength attribute. Any help/guidance will be appreciated.
Use it, this must work:-
<s:textarea name="someName" maxlength="100" class="form-control" rows="5"></s:textarea>
maxlength works with textarea in struts2
here, rows indicates default height of textarea i.e equal to 5 rows. if characters are more, it shows vertical-scroll bar on it.
Since <s:textarea/> is one of those Struts UI Tags that allow dynamic attributes, your problem turns into:
How to set maxlength for <textarea> tag in HTML?
And the solution is: use Javascript, checking the length during key / mouse events.
You can find a kick off example here.
try this
<s:textfield maxlength="300">
</s:textfield>
Okay. When you want to set the size of the textarea in struts2 follow bellow patter it may work
<s:textarea cols="5" rows="6"></s:textarea>
define size of it as you wish.
I have Three checkbox
<g:checkBox name="startAccountingStatus" value="Yes" />
<g:checkBox name="startAccountingStatus" value="No" />
<g:checkBox name="startAccountingStatus" value="NA" />
i want something like RadioGroup. Here now i can select all the checkBoxes. i dont want that..
I can check only one checkBox.
I can do it in jQuery which i dont want to ..
Is there any way of achieving it in Grails Gsp.
Why don't you go with HTML radio group, also present in Grails?
You can make radio items look like checkbxes.
If you want the check be made on the client browser and not checked at server side after submission the only way is via javascript (jquery, prototype or simple javascript it's little important)
I have a Rails form with a checkbox on a boolean field.
I want to replace the checkbox with a toggle button, using Twitter Bootstrap.
Is there anything baked into Rails to provide this kind of functionality? Or will I need to write a script to update the checkbox value when the toggle button is clicked?
Thanks for any pointers. After a lot of searching, I've been unable to find anything that describes this.
At this point in time, the toggle Bootstrap buttons do not have the correct markup to work out of the box with forms (source).
You can wrap around the controls to insert the correct values into your parameters map using hidden fields. If you're using AJAX requests, you can simply include them in your parameters there.
In the case of the single toggle, you could implement a function as simple as this to set the hidden value.
In your form:
<input name="toggled" value="false" />
Evaluated before submission:
function isToggled() {
$('input[name="toggled"]').value($('#toggleButton').hasClass('active'));
}
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>`