g:each iterator value not being applied in name of input - grails

So I've got a basic, g:each like so: -
<g:each in="${results}" status = "i" var="item">
<tr id = ${i} class="${(i % 2) == 0 ? 'even' : 'odd'}" name="main">
<td colspan="3">
<table id = "sub">
<tr>
<td><b>Action</b></td><td>
<g:select style="width:375px;"name="events[$i].id" from="${framework.EventType.list(sort:"userEventType")}" required="required" optionKey="id" value="${item.event_id}" /></td>
<td><b>Object</b></td>
<td>
<input type="text" name="any[$i].id">
<g:select style="width:550px;"id="objectID[$i]" name="objectID[$i].id" from="${framework.Object.list(sort:"objDesc")}" optionKey="id" required="" value="${item.object_id}" class="many-to-one"/>
<richui:autoComplete name="autocomp[$i].id" value= "${item.object_description}" action="${createLinkTo('dir': 'object/searchAJAX')}" maxResultsDisplayed="20" minQueryLength ="3" onItemSelect="youPickedThis(id,'autocomp[$i].id')" />
</td>
</tr>
</table>
</td>
</tr>
</g:each>
Absolutely everything within here is generated in the html with the correct names (i.e. [$i] becomes [0] on the first each, [1] on the second and so on. But the very first input (type text) just names them all "any[$i].id"!
Anyone have the remotest idea why the first input is too good to pick up up whereas the other elements just get on with it as they should?

The only difference I can see is that the <input type="text"> is a plain HTML tag whereas all the other places where you're using $i are attributes of GSP tags (richui:autocomplete and g:select). Try using ${i} instead:
<input type="text" name="any[${i}].id">
I wasn't aware that the short form (without using braces) was valid anywhere in a GSP, but the longer brace form will definitely work in all cases.

Related

Struts2 Hidden field within iterator

When using a hidden field within an iterator, why does it matter if the hidden field is within a tr tag? If it isn't it doesn't pass the value to the Action when the form is submitted.
For instance, the code below returns null to the Action for updatedData.transactionId...
<s:iterator value="adjustmentData" status="rowStatus">
<s:hidden name="updatedData[%{#rowStatus.index}].transactionId" value="%{transactionId}" />
<tr>
However the code below works....
<s:iterator value="adjustmentData" status="rowStatus">
<tr>
<s:hidden name="updatedData[%{#rowStatus.index}].transactionId" value="%{transactionId}" />
Why does it matter if the hidden tag is within the tr tag?
In each case, the field created is the same.
<tbody>
<input type="hidden" name="updatedData[0].transactionId" value="199984" id="updateAdjForm_updatedData_0__transactionId"/>
<tr>
<tbody>
<tr>
<input type="hidden" name="updatedData[0].transactionId" value="199984" id="updateAdjForm_updatedData_0__transactionId"/>

Grails - Edit - does not retain the values when created

Grails issue :
When clicking over the Edit in the following screen : Domain and Variable values does not seem to retain their values (When creating it, we are able to enter the values - Domain and Variable, but it does not retain the values when editing..
Clicking over EDIT in the following screen :
does not retain the values of domain and variable alone:
.gsp code below
<tr>
<td>Domain<span style="color: red">*</span></td>
<td><g:if test="${isCreate}">
<g:select class="statSele" id="domain_name" name="domain_name" from="${com.datumrite.master.DomainMaster.list()}" optionKey="id" noSelection="['':'Select Domain']" onchange="${remoteFunction(
action:'driveVariableFromDomain',
params:'\'id=\'+escape(this.value)',
update: 'variable_select'
)}"></g:select> <br>
</g:if>
<g:else>
<input disabled="true" id="dN" name="name" type="text" style="margin-bottom:4px" value="${(data.domain_name instanceof String)?'':com.datumrite.master.DomainMaster.get(data.domain_name)}" />
</g:else>
</td>
</tr>
<tr>
<td>Variable<span style="color: red">*</span></td>
<td><g:if test="${isCreate}">
<span id="variable_select">
<g:select class="statSele" name="var_name" from="${[]}" value="${data?.var_name}" noSelection="['':'Select Variable']"></g:select></span> <br>
</g:if>
<g:else>
<input disabled="true" type="text" name="name" style="margin-bottom:4px;margin-left:5px" value="${data?.var_name}" />
</g:else></td>
</tr>
<tr>
<tr>
<td>Condition<span style="color: red">*</span></td>
<td><g:if test="${isCreate}">
<g:select class="statSele" id="constrain_type" name="constrain_type"
from="${['Lesser than','Greater than','Lesser than nor Equal','Greater than nor Equal','Equal To','Not Equal To']}"
value="${data?.constrain_type}" noSelection="['':'Select Condition']">
</g:select>
</g:if>
<g:else>
<input disabled="true" name="name" value="${data?.constrain_type}" type="text" style="margin-bottom:4px"/>
</g:else></td>
</tr>
And, I am using MySQL DB for the same.
can anyone help me with this issue ?
You need to make sure the subscription instance is passed when you click Edit link. (possibly under <g:link...> tag) The gsp you've posted is for create view. Check your List of Subscription view. (possibly show.gsp)

Struts 2 select tag - Using the tag (with the same list) multiple times on a page does not work

I need to be able to use the same drop down list multiple times on a page. The first time i use the 'list' on the select tag, it works fine. The same list does not populate the second select tag i use it on. Here are the details.
In the action class, i populate the ArrayList containing values i need to populate the select tag.
setNames(new SomeDAO().getNames());
In the JSP
<s:select list="names"
id="nameList"
listKey="nameId"
listValue="userName"
/>
This select list populates just fine. If I use the following select tag on the same page (using the same list), it fails to print.
<s:select list="names"
id="rName"
listKey="nameId"
listValue="userName" />
If i replace the 'list' above with #{'test':'test'} (hardcoded list), the tag shows up fine. Looks like the property i set in the Action is getting cleared after the first use. Is that whats happening or am i doing something wrong? I get an 'IllegalStateException: Response already committed' error
Edit 1:
setNames() is used in the action method that deals with the JSP page. It is a simple getter function.
In the JSP, here is what i have.
<tr>
<td align="left" class="td-plain">Add New:</td>
<td class="td-plain">
<s:select list="names"
id="addNameID"
name="addUserNameID"
listKey="reinsId"
listValue="reinsName"
headerKey=""
headerValue="--User Name--"
/>
</td>
<td class="td-plain"><input id="addTreatyNumber" type="text" /></td>
<td class="td-plain"><input id="addReinsPercentage" type="text" /></td>
<td class="td-plain"><input id="addFlatDollarRetentionAmt" type="text" /></td>
<td class="td-plain">
<%if(finance){ %>
<input type="button" class="greyButton" value="Add" onclick="addReinsInfo()"/>
<%}else{ %>
None
<%} %>
</td>
</tr>
and then later down on that page, i have
<tr id='<s:property value="caseGroupId"/>:<s:property value="treatyId"/>'>
<td class="td-plain"><input type='checkbox' id='<s:property value="caseGroupId"/>:<s:property value="treatyId"/>'/></td>
<td class="td-plain">
<s:select list="names"
id="rName"
name="dName"
listKey="reinsId"
listValue="reinsName"
headerKey=""
headerValue="--User Name--"
/>
</td>
<td class="td-plain_"><s:textfield id="tNumber" value="%{treatyNumber}"/></td>
<td class="td-plain_"><s:textfield id="tPercentage" value="%{reinspercentage}"/></td>
<td class="td-plain_"><s:textfield id="rAmount" value="%{flatDollarRetentionAmt}"/></td>
<td class="td-plain"><input type="button" value="Delete" class="greyButton"/></td>
</tr>
If I change the 'list' in the second tag to
list="#{'Test':'Test'}"
the tag shows up fine. Please let me know if i can provide any further info.
Edit 2: I was able to get this to work by setting the drop down list values to the session.
As asked by #Dave and #Quaternion, post the relevant Java/JSP code.
But until then... I've noted that you are not using name attribute in Struts Selects;
while you can refer multiple times from different tags to the same source (the list attribute), to populate the Select from the same list of objects, you should instead specify a different name for each one, to define which variable (sent back to the Action) contains the selected value of which Select;
this may not be the answer to the current question but it will show up to you soon.

Access individual elements in a list

I have a list of, let's say, persons. Every person has a field that is "friend", that could be "yes" or "no". Their "friendship" status is presented in a column in the list:
<g:if test="${person.friend.status=='no' }">
<td><g:textField name="status" value="${person.friend.status}" readonly="readonly" style="width:60px;border:0px; background:transparent;color:black"/></td>
<td style="width:20px">
<g:actionSubmitImage action="acceptFriend" value="aceptar" src="${resource(dir: 'images', file: '/skin/tick_16.png')}" style="width:5px;height:8px;"/>
</td>
<td>
<g:actionSubmitImage action="refuseFriend" value="aceptar" src="${resource(dir: 'images', file: '/skin/wrong_16.png')}" style="width:5px;height:8px;"/>
</td>
</g:if>
As you can see, when the friend status is "no", two buttons are presented, one to say "that person is my friend" and another one to say "this person is not my friend". My question is:
When i click on the image that says "this person is my friend", I call the action "acceptFriend". But, how to access to the indivivual person instance, to change his status to "yes", and persist in the database? I think it may be with the "params" variable, but how to store it in the params to be accessed in the Controller?
Thank you!
I believe, you cannot use the g:actionSubmitImage like this, because in the tag's documentation is written:
You cannot use multiple actionSubmitImage tags within the same form and have it work in Internet Explorer 6 or 7 unless you add some custom JavaScript. See this page for a bit more information and a workaround.
The correct solution would be to have a form per friend in list and then you can easily pass the ID of the domain class.
The actionSubmitImage creates a submit button in some form.
Create the form for each person in the list you're iterating.
In that form, add a hidden field named "id" with a value="${person.id}", and in acceptFriend action read it from params.id.
<img src="${resource(dir: 'images', file: '/skin/wrong_16.png')}" style="width:5px;height:8px;" />
This is not tested code, just to give you the idea.
By request of Tom Metz, i post the snippet with both g:actionSubmitImage tags working:
<form>
<g:hiddenField name="contrato" value="${factura.id}"/>
<td> <g:link title="${message(code :'contratosVer.tooltip')}" controller="campaign" action="show" id="${factura.id}" style="width:160px"> <g:img dir="images" file="/skin/eye_16.png"/></g:link> </td>
<td><g:textField name="campaign" value="${factura.nombre}" readonly="readonly" style="width:120px;border:0px; background:transparent;color:black"/></td>
<td><g:textField name="total" value="${factura.presupuestosPendientes.total}" readonly="readonly" style="width:40px;border:0px; background:transparent;color:black"/></td>
<g:if test="${factura.estado=='Espera'}">
<td><g:textField name="estado" value="${factura.estado}" readonly="readonly" style="width:60px;border:0px; background:transparent;color:black"/></td>
<td style="width:20px">
<g:actionSubmitImage action="aceptarCamp" value="aceptar" src="${resource(dir: 'images', file: '/skin/tick_16.png')}" style="width:5px;height:8px;"/>
</td>
<td>
<g:actionSubmitImage action="rechazarCamp" value="aceptar" src="${resource(dir: 'images', file: '/skin/wrong_16.png')}" style="width:5px;height:8px;"/>
</td>
</g:if>
<g:else>
<td><g:textField name="estado" value="${factura.estado}" readonly="readonly" style="width:65px;border:0px; background:transparent;color:black"/></td>
<td></td><td></td>
</g:else>
<g:set var="desc" value="${result.toString()}" />
<g:if test="${desc.size() > 120}"><g:set var="desc" value="${desc[0..120] + '...'}" /></g:if>
</form>
Sorry for the delay, mate, but I was away from work for three days.

Iterator, set, if-else tags of Struts2(Getting the value of s:radio and applying if-else )

I have a jsp page in which I have two radio buttons and a select tag. Now, if the first radio button is clicked than I want to make that select tag disable but I am fail to do this , I have tried using "disabled" property. Following is the code.
Jsp Page
<table align="center">
<s:iterator value="FirstObjectList" status="AuthorTypeStatus">
<tr>
<td>
<s:radio name="radio_SelectedValue" list="{ObjectName}" listKey="ObjectKey" listValue="ObjectName" value="DefaultObject"/>
</td>
</tr>
</s:iterator>
<s:if test="%{#radio_SelectedValue == 'ObjectName1'}">
<s:set name="isSelectDisabled" value="false"/>
</s:if>
<s:else>
<s:set name="isSelectDisabled" value="True"/>
</s:else>
<tr >
<td colspan="2">
Select Parent Discover Lab
</td>
</tr>
<tr>
<td>
<s:select name="select_SelectedValue" headerKey="DefaultObject" headerValue="ParentObject" list="ObjectList" listKey="ObjectListKey" listValue="ObjectListValue" disabled="%{isSelectDiabled}">
<s:iterator value="ObjectList">
</s:iterator>
</s:select>
<s:submit value="Continue">
</s:submit>
</td>
</tr>
All the getter and setter methods are fixed..is this possible without javascript?
I'm a little confused by your code block, but I think I understand what you're aiming for. Something like this should work
<tr>
<td>
<s:radio name="radio_SelectedValue" list="%{radioList}" listKey="ObjectKey" listValue="ObjectName" value="DefaultObject"/>
</td>
</tr>
<s:if test="%{radio_SelectedValue.equals('myChosenValueFromTheList')}">
<s:select name="select_SelectedValue" headerKey="-1" headerValue=" " list="ObjectList" listKey="ObjectList.Key" listValue="ObjectList.Value" />
</s:if><s:else>
<s:select name="select_SelectedValue" headerKey="-1" headerValue=" " list="ObjectList" listKey="ObjectList.Key" listValue="ObjectList.Value" disabled="disabled" />
</s:else>
I'm not sure what you're doing with the iterators with these tags, they handle the list propagation themselves. Simply tell the tag what list to use and struts will populate the tags appropriately. You can then do a conditional based on the value of the radio when the page renders and perform magic on the select tag as shown above.

Resources