I want to get the value of a selectbox in gsp page on onChange event. I tried the following code:
<g:select id="resource-type" name="resource-type" from="${resultMap?.keySet()}" onchange="${remoteFunction(action:'updateDiffTable'
, update:'diff-table',params:[currentSelection:this.value,resultMap:resultMap])}">
I want to set the currentSelection parameter to the value of this selectbox. The problem is that I got null in current code. Any idea how to solve it?
try something like this:
<g:select
id="resource-type"
name="resource-type"
from="${resultMap?.keySet()}"
onchange="${remoteFunction(controller: 'mycontroller', action: 'action1', params:'\'id=\' + this.value')}" />
Related
When I attempt to use the grails datepicker, when I try out the page the value that gets sent to the controller is the default value, not the actual date value that I've chosen in the datepicker fields.
I'm using the grails datepicker in my simple index.gsp page:
<g:form action="show">
game ID: <g:textField name="gameId" value=""/><br/>
switch date: <g:datePicker name="switchDate" value="${new Date()}"/><br/>
<g:submitButton name="submit" value="submit"/>
</g:form>
my equally simple show controller:
def show(long gameId, Date switchDate) {
println("gameId:$gameId switchDate:$switchDate")
}
I'm guessing I'm missing something obvious. Thank you in advance.
I think this has to do with the Date not being bound properly. If you access it through params.switchDate or change the parameter to be of type String:
def show(long gameId, String switchDate) {
then it works.
Alternatively you can follow the steps from this question to set up proper data binding for the Date, i.e. by setting:
grails.databinding.dateFormats = ['MMddyyyy HH:mm:ss']
in your Config.groovy.
In my controller:
def billingDetails() {
def traineeDetails = session.traineeDetais
println "session data::"+traineeDetails
[traineeNames:traineeDetails.name]
}
This prints: [numberOfTrainees:2, submit_trainee_details:Next: Billing Details �, phone:[999999, 99999], email:[tester1#test.com, tester2#test.com], name:[Jack, Rob], jobTitle:[SE, SE], action:processTraineeDetails, controller:trainingOrder]
now in my GSP i want to have a select tag which will have name (jack, Rob) as options
<g:select name="traineeName"
from="${traineeNames}"
value=""
/>
which is not working fine.. How to make this to work so that i will get the names as options in dropdown list
make use of the optionKey optionValue fields in your g:select
reference:
http://grails.org/doc/2.2.1/ref/Tags/select.html
and do something like
<g:select optionKey="value" optionValue="value"
name="traineeName" from="${traineeNames}" />
I don't know the exact problem but one possible reason is that your session.traineeDetais.name is not a list. Check weather session.traineeDetais.name is a list or not, like
println "Check: ${session.traineeDetais.name instanceof List}"
if it is list then your code should work and if it is string then your drop down contain string characters as values.
been having toruble with the button that has action. I have several btns which I want to know its paramaeter. In grails tutorial it says it should be like this:
<g:actionSubmit action="action" value="${message(code: 'default.button.edit.label', default: 'Edit')}" params="['actionTaken':editPhone]"/>
I tried using remotelink, submitButton, submitToRemote tags but none works. I always get null when I try parsing it in my controller:
def action=
{
def actionTaken = params.actionTaken
def employeeId= params.employeeId
MySession session = MySession.getMySession(request, params.employeeId)
profileInstance = session.profileInstance
switch(actionTaken)
{
case "editPhone" :
isEditPhone=true
break
case "editEmail" :
isEditEmail=true
break
}
render(view:"profile", model:[profileInstance:session.profileInstance, isEditPhone:isEditPhone, isEditEmail:isEditEmail])
}
What am I missing? is my params code wrong? Is my code in parsing params wrong? this just gets me in circles with no progress. help. thanks.
The Grails documentation doesn't list params as one of the attributes accepted by actionSubmit.
It is possible to inject the value you want in your params list in the controller by exploiting what that tag actually does:
def editPhone = { forward(action:'action', params:[actionTaken: 'editPhone'])}
def editEmail = { forward(action:'action', params:[actionTaken: 'editEmail'])}
You may also just want to just code completely separate logic into the editPhone and editEmail actions if that makes your code cleaner.
Updated View Code:
<g:actionSubmit action="editPhone" value="${message(code: 'default.button.edit.label', default: 'Edit')}" />
The params attribute is parsed as a map, where the keys are treated a strings but the values are treated as expressions. Therefore
params="['actionTaken':editPhone]"
is trying to define the key named actionTaken to have the value from the variable named editPhone in the GSP model. Since there is no such variable you're getting null. So the fix is to move the quotes:
params="[actionTaken:'editPhone']"
which will set the value to the string "editPhone".
You could also pass the parameter inside the POST-data using
<input type="hidden" name="actionTaken" value="editPhone" />
inside the form. Then it is also accessible through the params variable.
It works for me.
I just had a similar problem (I needed to submit a delete together with an id) and found a solution using HTML5's "formaction" attribute for submit-inputs.
They can be given a value that can include a controller, action, additional parameters, etc.
In General, to add a parameter to a submit button such as a edit of a specific sub-object on a form would looks like this:
<input type="submit" formaction="/<controller>/<action>/<id>?additionalParam1=...&additionalParam2=..." value="Action" >
and in your example:
<input type="submit" formaction="action?actionTaken=editPhone" value="${message(code: 'default.button.edit.label', default: 'Edit')}" >
In my situation, I had a single form element with multiple actions on each row of a table (i.e. data table edit buttons). It was important to send the entire form data so I couldn't just use links. The quickest way I found to inject a parameter was with javascript. Not ideal, but it works:
function injectHiddenField(name, value) {
var control = $("input[type=hidden][name='" + name + "']");
if (control.size() == 0) {
console.log(name + " not found; adding...");
control = $("<input type=\"hidden\" id=\"" + name + "\" name=\"" + name + "\">");
$("form").append(control);
}
control.val(value);
}
Your button can look like this:
<g:each in="${objects}" var="object">
...
<g:actionSubmit value="edit" action="anotherAction"
onclick="injectHiddenField('myfield', ${object.id})"/>
</g:each>
I am trying to follow the ajax - driven select tutorial here: http://grails.org/AJAX-Driven+SELECTs+in+GSP however, I get the following error:
URI
/ajaxSelects/
Class
java.lang.NullPointerException
Message
Cannot invoke method list() on null object
I followed the tutorial exactly. The problem seems to be from the following code, where grails does not like Country.list():
<g:select
optionKey="id" optionValue="name"
name="country.nameid="country.name" from="${Country.list()}"
onchange="${remoteFunction(
controller:'country',
action:'ajaxGetCities',
params:'\'id=\' + escape(this.value)',
onComplete:'updateCity(e)')}"
></g:select>
Any ideas as to why this code is not working?
You either need to do a page import:
<%# page import="com.yourpackage.Country" %>
or use the full path for the list
from="${com.yourpackage.Country.list()}"
You also have mistyped here
name="country.nameid="country.name"
Should be
name="country.name" id="country.name"
I just read a lot and googled but I'm frustrated right now.
I have a Country domain model
class Country{
String countryCode //maybe EN, DE, CH...
}
Now I want a translation inside the . I read in the documentation (and with google) that it is possible with the "id" to select it from the translation message property files. Something like:
country.code.1=America
country.code.2=England
country.code.3=Germany
But this is not what I want. I want to have something like:
country.code.US=America
country.code.EN=England
country.code.DE=Germany
So, I found a possible solution from stackoverflow: translate a HTML select element in Grails
that would mean for me I have to put it like this:
<g:select name="country"
from="${allCountries}"
value="${country}"
optionKey="id"
optionValue="${ {countryCode->g.message(code:'country.code.'+countryCode)} }"/>
But my result is inside the dropdown: "country.code.grails.Country : 1" (and so on for each country)
If I change the last line of the gsp-g:select implementation to:
[...]optionValue="${ {countryCode->g.message(code:'country.code.US')}
as you see hardcoded! And THIS works :-D
Hope you got me and can help me, thank you very much!
There are 3 possibilities:
Instead of sending the id to the controller, use the contryCode instead of id:
<g:select name="contryByCountryCode"
from="${countryCodes}"
valueMessagePrefix="com.yourcompany"/>
Will produce:
<select name="contryByCountryCode" id="contryByCountryCode" >
<option value="US">United States<option>
...
</select>
If you have proper messages configured. In the backend you need to do something like:
def country = Country.findByCountryCode(params.contryByCountryCode)
Do it manually:
<select name="contryByCountryCode" id="contryByCountryCode" >
<g:each in="${countryCodes}" var="country">
<option value="${country.id}">
${message(code:"prefix" + country.countryCode)}
<option>
</g:each>
</select>
Patch g:select to work in case optionValue and messagePrefix is defined ;-)