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"
Related
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')}" />
Since we've updated to grails 2.0.1 (from 2.0.0) all of our beans shown via bean fields are incorrectly displayed as the first property of that "withBean" field. In the example I've posted below, all of [firstName, lastName, dateOfBirth, contactNumber] are shown as just 'firstName' (we know it's not just the messages which are wrong because otherwise the 3rd property (dateOfBirth) would be a date picker, not just a text field).
Any potential workarounds or suggestions?
(plugins.bean-fields=1.0-RC3)
I encountered the same problem, and have a work-around.
I has customised beanfield templates extracted into a gsp template called /shared/_beanfieldConfig.gsp , which I then included by rendering before calling any beans tags. e.g.
<g:render template="/shared/beanFieldConfig" />
<bean:withBean beanName='command'>
<bean:input property='username' />
This worked nicely in 1.3.7, and meant I could share beanFieldConfig between views.
When upgrading to 2.0.3, I enountered the same issue as the original question. I've found I can work around this by inlining the content of my _beanFieldConfig in each view.
Yuk, but at least it means I don't need rewrite all my views to use the replacement plugin (yet).
(edit)
Interestingly, although beanField config in a render'd template doesn't work, sticking it in a taglib DOES.
so, while previously I had in beanFieldConfig
<bean:inputTemplate>
<div class='input ${errors ? 'errors' : '' }'>
${label}
${field}
<g:if test="${errors}">
${errors}
</g:if>
</div>
</bean:inputTemplate>
If I define the equivalent tag -
def beanFieldConfig = {
bean.inputTemplate { m ->
m.with {
""" <div class='input ${errors ? 'errors' : '' }'>
${label}
${field}
${errors ?: ''}
</div>"""}
}
}
and then in my gsp replace <g:render template="/shared/beanFieldConfig" /> with <g:beanFieldConfig/>, it works.
I face a problem using Grails 2 submitToRemote tag.
The following code is what I use in the controller:
def getProposal = {
def layouts = importService.getLayoutsFor(params.product as int)
render(contentType: "text/xml") {
for (layout in layouts) {
option("${layout}")
}
}
}
and in the GSP:
<g:submitToRemote action="getProposal" update="layouts"
onLoading="showProgress();" onComplete="hideProgress();"
value="Do It" />
<select id="layouts" name="layout" required="">
</select>
Using jquery this results in:
showProgress();;jQuery.ajax({type:'POST',data:jQuery(this).parents('form:first').serialize(), url:'/app/controller/getProposal',success:function(data,textStatus){jQuery('#layouts').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){},complete:function(XMLHttpRequest,textStatus){hideProgress();}});return false
which not works and returns am error:
Node cannot be inserted at the specified point in the hierarchy
But if I use another render method like:
render(status: 0, text: "<option value='1'>Layout 1</option>")
it works.
In both cases the expected answer is transmitted back.
I did not understand why it will not work with the first nicer method. Could anyone explain what I do wrong?
Thx
Edit:
I noted that if I use render(contentType: "text/text") instead it will work. May be it has something to do, that the xml is not properly formatted (no root node?). But why does it work in Grails 1.3.7?
Grails 1.3.7 had a different default javascript provider (prototype). The jquery ajax call trys to infer the type of the response based on what it receives
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 ;-)
All,
Doing some experimenting with Spark and MVC within NerdDinner. The normal/aspx view works well, and I haven't touched any of the controller code so I'm pretty sure it's not that.
<viewdata model="System.Collections.Generic.IEnumerable[[NerdDinner.Models.Dinner]]"/>
<set Title="'Upcoming Dinners'"/>
<content:main>
<li each="p in Model">
!{Html.ActionLink(p.Title, 'Details', 'Dinners')}
</li>
</content:main>
Given the code above, the ActionLink gets rendered as http://serverName/Controller/Action/
Which is good. I start hitting a wall when I try to provide the ID to my action method. As far as I can tell from the Spark sample docs, I should be able to do something like this:
!{Html.ActionLink(p.Title, 'Details', 'Dinners', new {id = p.DinnerID} )}
However, that throws an exception:
" unexpected token '{' "
I'm hoping it's something silly I'm missing...any suggestions?
I believe there should be another parameter to Html.ActionLink for HTML attributes on the action link. Try:
!{Html.ActionLink(p.Title, 'Details', 'Dinners', new {id = p.DinnerID}, null )}