show a list of arrays of objects List(Object[]) in struts select tag <s:select> - struts2

<s:select label="Motif de la réclamation"
name="motifBean.motifId"
list="%{motifListBean}" listKey="motifId" listValue="libelle" value="1">
</s:select>
I use select tag like above but I don't know how to do it when i habe not keys and values but rather an Object[] inside the list
Thanks

Well, no answer. I converted the Obect[] to a Map
and used the map in the select tag

Related

how to set radio button for edit in grails

I am using grails 2.1.1 I have a domain where I want to save religion of member. I am saving it there is no problem. But when I go to edit page then it does not checked the original value.Suppose I am saving a religion for Buddhist and at the time of edit I want to be checked the value of it. But it is checking for Muslim. I want the field to be checked on edit page. Can anyone please help me on this please ??!!! Here are my attempts below ::
my domain class >>>
class RadioButton {
String religion
static constraints = {
}
}
my form page >>>
<div class="fieldcontain ${hasErrors(bean: radioButtonInstance, field: 'religion', 'error')} ">
<label for="religion">
<g:message code="radioButton.religion.label" default="Religion"/>
</label>
<g:radio name="religion" value="muslim" checked="checked"/> Muslim <br/>
<g:radio name="religion" value="hindu"/> Hindu<br/>
<g:radio name="religion" value="christian"/> Christian<br/>
<g:radio name="religion" value="buddhist"/> Buddhist
</div>
You may want to consider the radioGroup tag within Grails instead of manually authoring your radio buttons.
However, if you decide to continue manually authoring your radio buttons you will need to account for selecting the current value. For example:
<g:radio name="religion" value="muslim" checked="${radioButtonInstance?.religion.equals('muslim')}"/>
In the above example you will notice that the checked attribute is being set to a boolean value (which is correct according to the documentation).
I think radioGroup is by far a better solution for you as you are using grails.
They main problem is that you are not passing the currently set religion to the GSP. There is nothing telling the radio group which religion has already been set by the user, instead Muslim has been hard-coded with the checked="checked".
Judging from your first line which sets the class of the div if the bean has an error, I assume you can access the currently set religion from the radioButtonInstance. Using the radioGroup tag you pass the currently set value as ${radioButtonInstance?.religion}, then we set the values and the labels you need, as shown:
<g:radioGroup name="religion"
values="['Muslim', 'Hindu', 'Christian', 'Buddhist']"
labels="['Muslim', 'Hindu', 'Christian', 'Buddhist']"
value="${radioButtonInstance?.religion}">
<p>${it.label}: ${it.radio}</p>
</g:radioGroup>
I would, however, suggest that you set the available religions as an enum class rather than hard coding it onto the GSP, as you might want to reuse it. You could then pass it as a variable in the model through the controller, calling it, for example, religions, then your radioGroup would look like:
<g:radioGroup name="religion"
values="${religions}"
labels="${religions}"
value="${radioButtonInstance?.religion}">
<p>${it.label}: ${it.radio}</p>
</g:radioGroup>

How to get the selected value from drop down in the same jsp

I have one drop down list in jsp.
<s:select name="turboFilter.key" list="filterKeyList" listKey="key" listValue="key" />
Now i want the user selected value in another tag or in the jsp, say for ex, i wanted to print the user selected value in the same jsp, how to get it ? Please let me know.
Thanks in advance.

<g:countrySelect> Tag: How to set values as country names instead of country codes

On my view, instead of using a normal <g:textField> field in the form, I would like to make use of Grails' <g:countrySelect> widget.
My problem
<g:countrySelect name="country" value="${myObjInstance?.country}"/>
...produces...
<select name="country" id="country">
<option value="afg">Afghanistan</option>
<option value="alb">Albania</option>
...
</select>
This would mean that, when the form is submitted, it will assign the value as country code (e.g. "afg") instead of the country name ("Afghanistan"). I prefer using the full country name as value. If possible, how do I achieve this?
I don't believe the taglib as is gives you that ability. You would either need to extend it or use your own Country collection + <g:select />
Another option would be to use this enum of the ISO_3166 country codes to get the full length description. Eg:
CountryCode.getByCode(params.country).getName()
To answer my own question...
The values can stay as is (country codes) and then just use country codes. On your views, you can make use of the grails country() method to display the country name, e.g.
Let's say the country code is "afg" (the value saved in your table). On your view (GSP) you can use
<div>${country(code: fieldValue(bean: myObjInstance, field: "country"))}</div>
This will print out
<div>Afghanistan</div>
Instead of
<div>${fieldValue(bean: myObjInstance, field: "country")}</div>
...that was used before:
<div>afg</div>

GSP- Select tag. How to achive selected="selected"

I have a select tag in my GSP file as
<g:select name="clientId" id="clientId" size = "4" from="${com.springcommunity.fleet.partymodel.roles.ClientRole.list()}" class = "filter_combo" optionKey="id" />
i want client with id 2 is selected initially (in simple html it is achived by using selected="selected")
how can i do it?
You need to specify the value attribute in this tag. http://grails.org/doc/2.0.x/ref/Tags/select.html
So in your example,
<g:select ... value="${com.springcommunity.fleet.partymodel.roles.ClientRole.get(2)}" />
One thing to be aware of here is that the value that you're selecting must be an object equal to the item in the list, and not just an id - this is where a lot of people get tripped up. So you can't just say value='2', you need to specify the object in the list that you have in your from attribute.
From the docs -
value (optional) - The current selected value that evaluates equals()
to true for one of the elements in the from list.

Problem With Struts Select Default Value

I am using a LinkedList of a very simple custom class I created to fill the select statement. I got everything set properly (values and keys), but I can not load my default value.
This is the code I used for my select statement:
<s:select name="reports" list="filters" listKey="id" listValue="name" value="%{filterName}" onchange="filterResults(this.options[this.selectedIndex].text,this.options[this.selectedIndex].value)"/>
The filters list that is used has an id field (Integer) and a name field (String).
I know that the value in filterName should match with a value in the drop down list. I printed it to the screen with a property tag, but nothing is selected when the page loads. Any help is much obliged.

Resources