grails select with remoteFunction, can it update a g:textField? - grails

I need to update the value of a textField, using a value on the server, based on the value the user chooses in a g:select. In code:
<g:select name="description" from="${momentum.MoneyTransType.prodList}" value="${moneyInstance?.description}"
noSelection="['':'-Select Description-']" onChange="${remoteFunction(action:'setHowMuch', update:[success:'howMuch', failure:'failure'],
params:'\'selection=\' + this.value', options=[asynchronous:false])}"/>
<g:textField id="howMuch" name="howMuch" value="${moneyInstance?.howMuch}"/>
It's not working. If I give the "update:[success:" a div id, all is good, but that's not what I want. I need to allow the user to enter in a free flow description (which I'll have in another textfield), and a free flow amount. I guess I could hide a div and then listen for changes to that div via jQuery, and then update the amount textField. Should I be able to update a textField using the remoteFunction "update" capability or using another grails capability?
Strangely, putting in a temporary 'toHide' div with a jQuery change function isn't working to update the textField, i.e. the following alert, etc, isn't firing:
$('#toHide').change(function() {
alert(" I got changed, value:");
$("#howMuch").text($(this).val());
});

Well, after writing all the below, I reread your question and see you stated you know it works with a div. So the rest of my answer might not be helpful, but what's wrong with using a div? An empty div won't display anything, so you don't need to hide it. So FWIW:
Put your <g:textField ...> in a template.
Add a div where where you want the template to be rendered. In other
words, replace the current <g:textField ..> with <div id=updateme name=updateme></div>
In your setHowMuch action, render the template.
For example, I do:
In view:
<g:select class='setTagtypeValue-class'
name='tagtype-${i}-header'
from="${org.maflt.ibidem.Tagtype.list(sort:'tagtype').groupBy{it.tagtype}.keySet()}"
value="${setTagtypeValue?.tagtype?.tagtype}"
valueMessagePrefix="tagtype"
noSelection="${['null':'Select One...']}"
onchange="${remoteFunction(action:'options', update:'tagtype-options-${i}',
params:'\'tagtype=\' + this.value +\'&i=${i}\'' )}" />
Controller action:
def options = {
def i = params.i ?: 0
def tagtypes = Tagtype.findAllByTagtype(params.tagtype)
render(template:"tagtypeList", model:[tagtypes:tagtypes,i:i])
}
tagypeList Template:
<table>
<tr>
<th></th>
<th><g:message code="tagtype.lookup"
default="Lookup Table" /></th>
<th><g:message code="tagtype.regexpression"
default="Field Rule" /></th>
<th><g:message code="tagtype.uicomponent"
default="UI Component" /></th>
</tr>
<g:each in="${tagtypes}" var="tagtype" status="j">
<tr>
<td><g:radio name="setTagtypesList[${i}].tagtype.id" value="${tagtype.id}"
checked="${(setTagtypeValue?.tagtype?.id == tagtype.id ||
(!setTagtypeValue?.tagtype?.id && j==0))}"></g:radio></td>
<td>${tagtype.lookupTable}</td>
<td>${tagtype.regexpression}</td>
<td><g:message code="${'uicomponent.' + tagtype.uicomponent.id}"
default="${tagtype.uicomponent.uicomponent}" />
</td>
</tr>
</g:each>
</table>
This code is from the metadataset (called field set in the UI) screen in http://www.maflt.org/products/Ibidem.

Related

Pagination resets checkbox of rows

I have a GSP containing some rows with an checkbox which lets the user select several rows.
<table>
<thead>
<tr>
// Column headers
</tr>
</thead>
<tbody>
<g:each in="${itemList}" status="i"
var="instance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
// Some other rows
<td>
<g:checkBox name="selected"
value="${instance.id}"
checked="false" />
</td>
</tr>
</g:each>
</tbody>
And a pagination under my table:
<div class="pagination">
<g:paginate total="${total}" params="${params}"/>
</div>
Now the problem is when I switch from Page 1 -> Page 2 -> and then back to Page 1 all the checkboxes from Page 1 are reseted.
As pagination calls the list controller method I checked the following on page switch by Watching the following in Debugger when list controller method gets called:
params.list('selected')
But unfortunately the list is empty.
Pagination does not submit a form (or at least not like you're thinking it does; I really don't remember if the underlying logic uses a form or not) so your checkboxes are not being submitted to anywhere.
You need to consider different approaches for having your form submitted so that your checkbox selections are persisted. Two approaches could be:
Write your own pagination tag that wraps g.paginate and modifies which params are sent so that your checkbox values are included.
Add ajax handling to your checkboxes so that they are submitted immediately when changed.

Thymeleaf - if checked do something

I have a simple table and I have checkbox inside that table. If checkbox is selected I want to display other features.
My code:
<table>
<tr th:each="obj,iterationStatus : ${objs}">
<td th:text="${obj.name}"></td>
<td><input type="checkbox" name="firstcheckbox"></td>
<td th:if="isChecked">
<!-- do something -->
</td>
</tr>
</table>
How I do this without using javascript and backbean?
In this case you have two options, the first option you don't want but is not a bad option is using Javascript: creating a function called isChecked.
Otherwise, you can know if a checkbox is checked with thymeleaf in the case you are in a form which calls itself. You could do something like if(firstcheckbox != null) and, of course, your checkbox would have a value <input type="checkbox" name="firstcheckbox" value="value">. Finally, you must add as attribute in your model: model.addAttribute("firstCheckbox", firstCheckbox);

Capybara: Pulling in multiple Radio button options

Im running into an issue, and I think the issue is with how my page.all is pulling radio button questions in.
So here is the HTML for the table itself (Multiple questions with 5 radio button choices a piece):
<table class="table table-striped table-stuff table-collapsible">
<colgroup>
<thead>
<tbody>
<input id="0_answer_question_id" value="9966" name="response[answers][0][answer_id]" type="hidden">
<tr>
<td class="heading">
<td class="option">
<div class="radio-inline radio-inline--empty">
<input id="question_1_1" value="1" name="response[answers_attributes][0][answer_opinion]" type="radio">
<label for="question_1_1">Strongly Disagree</label>
</div>
</td>
<td class="option">
<td class="option">
<td class="option">
<td class="option">
</tr>
<input id="response_1_question_id" value="9966" name="response[answers_attributes][1][answer_question_id]" type="hidden">
<tr>
<input id="response_1_id" value="<a number>" name="response[answers_attributes][1][id]" type="hidden">
<Same as above repeated 5 times with numbers changed>
</tbody>
</table>
Im using:
page.all('table.table-stuff tbody tr', minimum: 6).each do |row|
row.all("td label").sample.trigger('click')
end
To get each row and select one from it. HOWEVER, I notice "sometimes" a row will not have one selected. My theory is the "heading" (which has a <label> itself is accepting one of the clicks perhaps? (since from my understanding of how page.all works it's grabbing every tbody tr within the table...but is maybe grabbing the heading too? (since it contains a td label?)
Also when a table is named something like table table-striped table-stuff table-collapsible...how can you tell what the actual table "name" is? (I didn't write this website, just doing tests for it). When putting it in the page.all('table.<etc>')?
If the heading td (it's not expanded in your example) also contains a label element (so it would be included in the results of your all call) then you just need to change the CSS selector so it wouldn't be included - something like
row.all("td.option label").sample.trigger('click') # only choose labels contined in tds with the class of 'option'
or
row.all("td:not(.heading) label").sample.trigger('click') # choose labels contained in tds without the class of 'heading'
On your second question about table names, I don't really understand what you're asking. Tables don't have name attributes, they could have an id attribute or a caption containing some text which could then be used to find them with capybara via find(:table, 'id or caption text') or within_table('id or caption text') { code to execute within scope of the table }. Rather, you seem to be talking about the classes on the element which are specified in a CSS selector with '.'. Therefore a CSS selector to match a table element with all the classes you listed would be - 'table.table.table-striped.table-stuff.table-collapsible'
Note: If you're sure there's always only 5 choices you could add the :count option to your find to make sure your selector is only finding those items
row.all("td.option label", count: 5).sample.trigger('click')

Empty string in Bean not overwriting previous value in h:inputText

JSF 2 problem. I have a hidden edit area on my form. When the user clicks the Edit button, I set a bean property to true to display the area (this is a bit of a simplification) and set another bean property to the value being edited. This variable is referred to like:
<h:inputText value="#{bean.stringValue}" />
When the user cancels out of editing, I hide the edit area.
Let's say the user then wants to edit another item, but this one's value is the empty string (""). Using the same code, stringValue is set to the emptyString and the area displays.
However, the value from the previous edit is displayed, not an empty text box.
Without resorting to JavaScript, is there a way to make this work?
Edit: Following is as close as I can come to a SSCCE. As you see, I am activating in-place editing inside a table. I'm also using ICEfaces, but nothing special in this case.
<table>
<tbody>
<ui:repeat var="cfi"
value="#{evDetailBean.completeEvent.listCompleteCashFlowItems}">
<ice:panelGroup rendered="#{!cfi.editThisOne}">
<tr>
<td>#{cfi.cfiName}</td>
<td>#{cfi.absOfAmount}</td>
<td>#{cfi.frequencyDescr}</td>
<td>#{cfi.cfiToFrom}</td>
<td>#{cfi.assetPoolName}</td>
<td style="text-align: center"><h:commandLink
actionListener="#{cfi.editCfiListener}" value="Edit" />    <h:commandLink
value="Delete" actionListener="#{cfi.deleteCfiListener}" />
</td>
</tr>
</ice:panelGroup>
<ice:panelGroup rendered="#{cfi.editThisOne}">
<tr>
<td><ice:inputText value="#{evDetailBean.newCFIName}"
style="width:118px;" partialSubmit="true" immediate="true" validator="#{evDetailBean.valNewCFIName}" /></td>
<td>xxx</td>
<td>xxx</td>
<td>xxx</td>
<td>xxx</td>
<td style="text-align: center;"><ice:commandButton
value="Save" immediate="true"
actionListener="#{evDetailBean.saveEditCfiListener}"
styleClass="plumvo-button"
style="float:left; vertical-align: middle;" />   
<ice:commandLink value="Cancel" style="vertical-align: middle;"
actionListener="#{cfi.cancelEditCfiListener}" /></td>
</tr>
</ice:panelGroup>
</ui:repeat>
</tbody>
And this is the actionListener (in part):
public void editCfiListener(ActionEvent e) {
EvDetailBean evDetailBean = completeEvent.getEvDetailBean();
evDetailBean.setNewCFIName(this.getCfiName());
// initialize more fields
editThisOne = true; // This causes the row being edited to open up with modifiable fields.
}
Thanks in advance for your help.
Just check in your bean after submit if stringValue is "" and if so, set it to null and visible property to false.
the action , the bean function you are calling on click of edit button. inside that clear the stringValue variable.

Grails Remote Function and Ajax

I am trying to list addresses and onChange event I send the address ID and return a list of users.
<g:select name="AddressID" from="${address}" optionKey="id" optionValue="address"
onchange="${ remoteFunction(
action:'testMe',
params:'\'id=\' + this.value' ,
update:'show' )}">
</g:select>
I call this div "show".
<div id="show">
<g:each in="${users}" status="i" var="C">
<h3>${C}</h3>
</g:each>
</div>
However, when I click the item on the list box I get the following error:
description The requested resource (/MyTest/WEB-INF/grails-app/views/test/testMe.jsp) is not available.
My understanding of an update attribute inside a remoteFunction is that it is the name of the div which gets refreshed after the remoteFunction call is compeleted.
Thanks.
I would reorganize your code a little to get the Ajax working:
Move your div contents into a partial template, called, say, _remoteResponse.gsp:
<g:each in="${users}" status="i" var="C">
<h3>${C}</h3>
</g:each>
Modify your controller's testMe action to render the response to a template, something like:
render (template:'remoteResponse', model:[users:['mike,tim']])
Make sure you have an ajax library setup in main.gsp, like:
<g:javascript library="prototype" />

Resources