a List of objects disappeared after Rendering a view - grails

Dear grails specialists
I'm sending a list of publications to my gsp view page. I can see the list well, but after adding one of the publications into the books list and rendering the publications gsp view page, the publications list is not seen as a list of publications but as a String. So when I try to get each publication it gives me the chars.
I am using the version of grails 2.2.0.
Here i send publications in controller Entries:
def books{
def unit = Unit.get(params.id)
def period = Period.get(params.periodId)
def publications = []
if (unit) {
def units = unit.downHierarchy().id
units.removeAll { null }
publications = Publication.publicationsOfUnits(units)?.listDistinct()?.findAll{it.date.getAt(Calendar.YEAR) == period.year}?.sort{ it.date }
}
[publications : publications, id: unit.id, unitId: unit.id, periodId: period.id]
}
and here is my view page:
<table>
<g:each in="${publications}" var="p">
<g:if test="${p instanceof publicationBook}">
<tr>
<td>${p.authors.join(",")}</td>
<td>${p.title}</td>
<td>
Add to Books
</td>
</tr>
</g:if>
</g:each>
</g:table>
here i add the publication to books:
def saveBook = {
saveEntry("book", new Book())
render(view:"books", model: params)
}
and here is the book Form where I send the list of publications again to controller for rendering:
<g:form name="bookForm" controller="entries" action="saveBook" class="default" method="post">
<g:hiddenField name="entryId" value="${entry?.id}"/>
<g:hiddenField name="unitId" value="${unitId}"/>
<g:hiddenField name="periodId" value="${periodId}"/>
<g:hiddenField name="publications" value="${publications}"/>
<div class="buttons">
<button class="button icon icon_arrow_right leave_empty" onclick="$('#leaveEmpty').val('true');bookForm.submit();"
</div>
</g:form>
I would very appreciate any advise or help to solve the problem..

You are NOT passing the proper model here render(view:"books", model: params).
Either use redirect(action:"books", id: id), or fill the model with the publications list etc., as you are doing in the books action

Related

Grails, how can I pass list of object from gsp to controller

I have a list of csv files that will be imported in database.
So, in the first step, I display the name of files in the jsp page and then I wait the choice of user what's the file need to import it or to ignore it.
when the user confirms his response, I need to pass the list of files that user has chosen to import it to controller.
I thought about that : I set the list that contains a list of file in hidden field and i will recuperate it into controller action from form submit. But in the controller, it is read like a string variable and I can not extract data from it.
<g:hiddenField id="list_file_notimported" name="list_file_notimported" value="${list_file_notimported}" />
<table>
<g:findAll in="${list_file_notimported}" expr="1" >
<tr>
<td></td>
<td>${it.code}</td>
<td>${it.name}</td>
<td><g:radio id="group_${it.id}" name="group_${it.id}" value="import" checked="${false}" /></td>
<td><g:radio id="group_${it.id}" name="group_${it.id}" value="ignore" checked="${false}" /></td>
</tr>
</g:findAll></table>
Any idea please?
Thanks.
This example works in Grails 2.4.3:
In the controller, define index action to return a model and selection action to compute the list of files selected in the gsp:
class FileListController {
def index() {
[ list_file_notimported : [ 'a.csv', 'b.csv', 'c.csv', 'd.csv'] ]
}
def selection() {
def selectedfiles = []
params.keySet().each { String key ->
if (key.startsWith("group_") && key.endsWith(".csv") && params[key] == "import") {
selectedfiles << key.substring(6)
}
}
render(selectedfiles)
}
}
And in the view, create a form for the selection:
<g:form action="selection" method="get">
<table>
<tr><th>Name</th><th>Import</th><th>Ignore</th></tr>
<g:each var="it" in="${list_file_notimported}" >
<tr>
<td>${it}</td>
<td><g:radio name="group_${it}" value="import"/></td>
<td><g:radio name="group_${it}" value="ignore" checked="true"/></td>
</tr>
</g:each>
</table>
<g:actionSubmit value="selection"/>
</g:form>

Populate Grails g:select from function

Trying to populate a g:select from a function in my controller, is it possible?
Currently we have this:
def run(Long id) {
def reportInstance = Report.get(id)
def listPromptValues = populatePrompts(reportInstance)*.values()
if (!reportInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'report.label', default: 'Report'), id])
return
}
[reportInstance: reportInstance, listPromptValues: listPromptValues]
}
def populatePrompts(Report rp){
//for each prompt in the report, go out and get it's values
rp.prompts.collectMany {
reportService.listDatatypeValues(it.datatype)
}
}
And then the g:select in question is
<li class="fieldcontain">
<g:each var="prompt" in="${reportInstance.prompts}">
<span id="prompts-label" class="property-label">
<g:message code="report.prompts.label" default="${prompt.name}:" />
</span>
<g:if test="${prompt.datatype.type == 'DropDown'}">
<g:select id="prompt.name" from="${listPromptValues}" name="prompt.name" value="" noSelection="['':'']"/>
<br>
</g:if>
</g:each>
</li>
Which works just fine if there is only one prompt, however we need to be able to call the populatePrompt function directly from the gsp view if there is more than one prompt in the loop, possible sending the reportId and then getting back the listPromptValues. I can't seem to get the onChange(remoteFunction...) to work properly and am coming up empty handed in searching the vast Google webs.
Something like how createLink works
${createLink(controller:'report', action:'runReport', id:"${reportInstance.id}")}
But instead of createLink, it would be the from attribute of the select tag, something like:
<g:select id="prompt.name" from="{(controller:'report',action:'populatePrompt', id:"${reportInstance.id}")}" name="prompt.name" value="" noSelection="['':'']"/>
Any ideas, or direction to go?
I think #JamesKleeh proposed a viable solution in his last comment.
Given the fact that your gsp structure is quite static, it doesn't make sense to fetch the prompt select options to be dynamically loaded. Just return these options in a List package within listPromptValues from your controller and take it in the gsp directly.
Concerning your parameters like [prompt1: ['a','b','c'], prompt2: ['d','e','f']], you can get this map in your populatePrompts method and put each key-value pair into gsp select tags. Like this:
controller
{ ....
def listPromptValues = populatePrompts(reportInstance)
....
}
def populatePrompts(Report rp){
//for each prompt in the report, go out and get it's values
def promptMap = [:] //map to be returned
rp.prompts.each {
promptMap.put(it.name, reportService.listDatatypeValues(it.datatype))
}
return promptMap
}
gsp
<g:each var="prompt" in="${reportInstance.prompts}">
<span id="prompts-label" class="property-label">
<g:message code="report.prompts.label" default="${prompt.name}:" />
</span>
<g:if test="${prompt.datatype.type == 'DropDown'}">
<g:select id="prompt.name" from="${listPromptValues[prompt.name]}" name="prompt.name" value="" noSelection="['':'']"/>
<br>
</g:if>
</g:each>

Update Drop Down Value in Grails using <g:submitToRemote>

I have just entered into the Grails arena.
I have a requirement where I want to put one drop down and one button on one page, and by clicking on button only that drop down values should be changed, other controls on the page should remain unchanged.
My code is as follows :
_connType.gsp
<div id="mappedDeviceDiv">
<select id="mappedDevice" name="mappedDevice" value="" style="width:200px">
<g:each in="${deviceList}" status="i" var="dl">
<option value="${dl}">${dl}</option>
</g:each>
</select>
<g:submitToRemote class="blackButton" update="mappedDeviceDiv"
url="${[controller:'resource',action:'getDeviceList']}"
value="Get Devices"/>
</div>
ResourceController.groovy
def getDeviceList = {
println "Getting NV devices.. + Nirmal" + params
def model = buildAccessRequestModel()
List<NVDeviceBean> deviceList = NVUtil.getDevices(params.datasource, null);
Collections.sort(deviceList);
List<String> devices = []
for(NVDeviceBean deviceBean : deviceList) {
devices.add(deviceBean.getName())
}
println "list = "+devices
model.putAt('deviceList', devices)
render (template:'config/connType',model:model)
}
So, in above scenario, it's setting the values in the devices perfectly, but at the view side in a drop down m getting whole connType page, instead of only list values which is in devices variable of controller.
Any help would be highly appreciated..
You might want to create/modify one of the controller actions to return a list of HTML options. You could even have this action render a template, too.
def getDeviceOptions = {
def options = []
// code that creates the option list goes here.
render(template:'config/optionList', model: [optionList: options ])
}
And the template...
<!-- config/_optionList.gsp -->
<g:each in="${optionList}" status="i" var="dl">
<option value="${dl}">${dl}</option>
</g:each>
Then, tell the g:submitToRemote tag to update the select object.
<g:submitToRemote class="blackButton" update="mappedDevice"
url="${[controller:'resource',action:'getDeviceOptions']}"
value="Get Devices"/>
That should get you started in the right direction.

Grails: filter data in a Grails table dynamically

I have a table, with a series of events,
name of my class is Entry.
Here is a picture of my table
is in Spanish, but the basics are the same so it shouldn't be a problem.
(the filter HTML code is not yet so its a mess but it works)
and here are the results that I get when I look for the entry.
Now my problem is that I need the results to be shown/filtered into the same table from pic1, so it would be basically like a table update applying the filters.
If you need more info here is the link to my old question. Thanks proflux!
I have a bunch of data and I need a data filter using Grails
Most of the search code is there,
Any help would be greatly appreciated!
UPDATE:
I have a problem filtering the dates though... I have two dates...
One is the date the event is going to take place, and the other one is lastUpdated which I think is a keyword for grails for the last time you modified the Event. Any help related to filtering dates would be greatly appreciated.
I need to show everything on the first table starting from today's date. And if I want to find something from the past I should be able to use the filter to find it.
Any ideas on how to do this?
UPDATE:
So here is my list.gsp
and here is my searchResults.gsp with the filters applied for the word "Ruta"
So basically everything looks nice and pretty but the date filters are not working.
Here is the code in the controller that is not filtering the dates
def searchResults = {
def entryCriteria = Entry.createCriteria()
def results = entryCriteria.list {
if(params?.proyectoRuta) {
ilike("proyectoRuta","%${params.proyectoRuta}%")
}
}
if(params?.day) {
eq("fechaCambio", params.day)
}
render(view:'searchResults', model:['results':results])
}
is filtering the word but not the dates
proyectoRuta would be the title and fechaCambio would be the date shown in the first column. I have not tried to filter the lastUpdated date yet.
UPDATE:
Ok so here is my controller: Since is a lot of code I will only post the important defs
def search = {
render(view:'search')
}
def searchResults = {
def entryCriteria = Entry.createCriteria()
def results = entryCriteria.list {
if(params?.fechaCambioD && params?.fechaCambioH) {
between("fechaCambio", params.fechaCambioD, params.fechaCambioH)
}
if(params?.lastUpdatedD && params?.lastUpdatedH) {
between("lastUpdated", params.lastUpdatedD, params.lastUpdatedH)
}
if(params?.proyectoRutaN) {
ilike("proyectoRuta","%${params.proyectoRutaN}%")
}
}
render(view:'searchResults', model:['results':results, 'proyectoRutaN':params?.proyectoRutaN, 'fechaCambioD':params?.fechaCambioD, 'fechaCambioH':params?.fechaCambioH, 'lastUpdatedD':params?.lastUpdatedD, 'lastUpdatedH':params?.lastUpdatedH])
}
}
And here is the searchResults.gsp
<tbody>
<g:each in="${results}">
<td><g:formatDate format="dd-MMMM-yyyy" date="${it.fechaCambio}" /></td>
<td><b>${it.proyectoRuta}</b></td>
<td>${it.summary}</td>
<td><g:formatDate format="dd-MMM-yyyy HH:mm z" date="${it.lastUpdated}" /></td>
<td>
<g:form>
<g:hiddenField name="id" value="${it?.id}" />
<span class="simple"><g:actionSubmit class="editar" action="edit" value="${message(code: 'default.button.editar.label', default: ' ')}" /></span>
<span class="simple"><g:actionSubmit class="eliminar" action="delete" value="${message(code: 'default.button.eliminar.label', default: ' ')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Esta seguro que desea Eliminar?')}');" /></span>
</g:form>
</td>
</tr>
</g:each>
</tbody>
Try this using the same gsp for both the results and the search criteria...
<html>
<body>
<h1>Search Criteria</h1>
<g:form controller="entry" action="searchResults">
Title: <g:textField name="title" value="${title}" />
Date: <g:datePicker name="startTime" value="${startTime}" precision="day" />
<g:submitButton name="submit" value="Search" />
</g:form>
<g:if test="${results}">
<h1>Search Results</h1>
<table>
<tr>
<th>Title</th>
<th>Start Date</th>
</tr>
<g:each in="${results}">
<tr>
<td>${it.title}</td>
<td>${it.startTime}</td>
</tr>
</g:each>
</table>
</g:if>
</body>
</html>
And then for your controller closures:
def search = {
render(view:'search')
}
def searchResults = {
def entryCriteria = Entry.createCriteria()
def results = entryCriteria.list {
if(params?.title) {
ilike("title", "%${params.title}%")
}
}
render(view:'search', model:['results':results,
'title':params?.title,
'startTime':params?.startTime])
}
This is from a quick demo i wrote really quick, but from the info I gave you on the other question and the Grails Reference Documentation on using criteria queries, you should be able to figure it out. Let me know if you run into any problems.
Update:
Try something like this for your between criteria:
def entryCriteria = Entry.createCriteria()
def results = entryCriteria.list {
if(params?.title) {
ilike("title", "%${params.title}%")
}
if(params?.day) {
between("day", params.day, params.day+1)
}
}
Sounds like you want to show the search results in a 'list' view, so they show up in a table just like when unfiltered. You can just reuse that view and pass in the filtered results as the instance list.
Do you have views for your Entry domain object? If not, generate some scaffolding views with grails generate-view Entry. Then in your controller, make your searchResults method look something like this:
def searchResults = {
def entryInstanceList = Entry.list() // replace with actual filtering logic
render(view:'list', model: [entryInstanceList: entryInstanceList])
}

Grails - Simple hasMany Problem - Using CheckBoxes rather than HTML Select in create.gsp

My problem is this: I want to create a grails domain instance, defining the 'Many' instances of another domain that it has. I have the actual source in a Google Code Project but the following should illustrate the problem.
class Person {
String name
static hasMany[skills:Skill]
static constraints = {
id (visible:false)
skills (nullable:false, blank:false)
}
}
class Skill {
String name
String description
static constraints = {
id (visible:false)
name (nullable:false, blank:false)
description (nullable:false, blank:false)
}
}
If you use this model and def scaffold for the two Controllers then you end up with a form like this that doesn't work;
My own attempt to get this to work enumerates the Skills as checkboxes and looks like this;
But when I save the Volunteer the skills are null!
This is the code for my save method;
def save = {
log.info "Saving: " + params.toString()
def skills = params.skills
log.info "Skills: " + skills
def volunteerInstance = new Volunteer(params)
log.info volunteerInstance
if (volunteerInstance.save(flush: true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'volunteer.label', default: 'Volunteer'), volunteerInstance.id])}"
redirect(action: "show", id: volunteerInstance.id)
log.info volunteerInstance
}
else {
render(view: "create", model: [volunteerInstance: volunteerInstance])
}
}
This is my log output (I have custom toString() methods);
2010-05-10 21:06:41,494 [http-8080-3] INFO bumbumtrain.VolunteerController - Saving: ["skills":["1", "2"], "name":"Ian", "_skills":["", ""], "create":"Create", "action":"save", "controller":"volunteer"]
2010-05-10 21:06:41,495 [http-8080-3] INFO bumbumtrain.VolunteerController - Skills: [1, 2]
2010-05-10 21:06:41,508 [http-8080-3] INFO bumbumtrain.VolunteerController - Volunteer[ id: null | Name: Ian | Skills [Skill[ id: 1 | Name: Carpenter ] , Skill[ id: 2 | Name: Sound Engineer ] ]]
Note that in the final log line the right Skills have been picked up and are part of the object instance. When the volunteer is saved the 'Skills' are ignored and not commited to the database despite the in memory version created clearly does have the items. Is it not possible to pass the Skills at construction time? There must be a way round this? I need a single form to allow a person to register but I want to normalise the data so that I can add more skills at a later time.
If you think this should 'just work' then a link to a working example would be great.
If I use the HTML Select then it works fine! Such as the following to make the Create page;
<tr class="prop">
<td valign="top" class="name">
<label for="skills"><g:message code="volunteer.skills.label" default="Skills" /></label>
</td>
<td valign="top" class="value ${hasErrors(bean: volunteerInstance, field: 'skills', 'errors')}">
<g:select name="skills" from="${uk.co.bumbumtrain.Skill.list()}" multiple="yes" optionKey="id" size="5" value="${volunteerInstance?.skills}" />
</td>
</tr>
But I need it to work with checkboxes like this;
<tr class="prop">
<td valign="top" class="name">
<label for="skills"><g:message code="volunteer.skills.label" default="Skills" /></label>
</td>
<td valign="top" class="value ${hasErrors(bean: volunteerInstance, field: 'skills', 'errors')}">
<g:each in="${skillInstanceList}" status="i" var="skillInstance">
<label for="${skillInstance?.name}"><g:message code="${skillInstance?.name}.label" default="${skillInstance?.name}" /></label>
<g:checkBox name="skills" value="${skillInstance?.id.toString()}"/>
</g:each>
</td>
</tr>
The log output is exactly the same! With both style of form the Volunteer instance is created with the Skills correctly referenced in the 'Skills' variable. When saving, the latter fails with a null reference exception as shown at the top of this question.
Hope this makes sense, thanks in advance!
Gav
Replace your create.gsp <g:checkbox...> code by:
<g:checkBox name="skill_${skillInstance.id}"/>
Then inside the save action of your controller, replace def volunteerInstance = new Volunteer(params) by :
def volunteerInstance = new Volunteer(name: params.name)
params.each {
if (it.key.startsWith("skill_"))
volunteerInstance.skills << Skill.get((it.key - "skill_") as Integer)
}
Should work. (code not tested)
I would reader send id list of your has many elements because this can be easily assigned by default in Grails.
Your .gsp should look like:
<g:each in="${skills}" var="skill">
<input type="checkbox"
name="skills"
value="${skill?.id}"
</g:each>
and in your controller you can simply stores the value like this:
person.properties = params
person.validate()
person.save()
It's pretty easy, isn't it? :-)
Grails does not provide data-binding support when you use a checkbox and you want to bind ToMany associations. At least, up to version 2.2.0
Workaround ?
1º option - Write gsp code which behaves like a select component
<g:each var="skillInstance" in="${skillInstanceList}">
<div class="fieldcontain">
<g:set var="checked" value=""/>
<g:if test="${volunteerInstance?.skills?.contains(skillInstance)}">
<input type="hidden" name="_skills" value="${skillInstance?.id}"/>
<g:set var="checked" value="checked"/>
</g:if>
<label for="${skillInstance?.name}">
<g:message code="${skillInstance?.name}.label"
default="${skillInstance?.name}" />
</label>
<input type="checkbox" name="skills" value="${skillInstance?.id}"
${checked} />
</div>
</g:each>
2º Create your own TagLib
/**
* Custom TagLib must end up with the TagLib suffix
*
* It should be placed in the grails-app/taglib directory
*/
class BindingAwareCheckboxTagLib {
def bindingAwareCheckbox = { attrs, body ->
out << render(
template: "/<TEMPLATE_DIR>/bindingAwareCheckboxTemplate.gsp",
model: [referenceColletion: attrs.referenceColletion,
value:attrs.value])
}
}
Where <TEMPLATE_DIR> should be relative to the /grails-app/views directory. Furthermore, templates should be prefixed with _.
Now you can use your custom TagLib as follows
<g:bindingAwareCheckbox
referenceCollection="${skillInstanceList}"
value="${volunteerInstance?.skills}"/>
Once done, binding process will occur automatically. No additional code needed.
GSP
<g:checkBox name="skills" value="${skillInstance.id}" checked="${skillInstance in volunteerInstance?.skills}"/>
Groovy
def volunteerInstance = new Volunteer(params).save()
def skills = Skill.getAll(params.list('skills'))
skills.each{ volunteerInstance.addToSkills(it).save() }

Resources