GSP Access to Controller Results - grails

I am using the Dojo plugin for Grails to populate a dojox.grid.DataGrid object. Right now my controller action associated with this grid renders the JSON that defines the grid contents. That works just fine as long as I predefine the columns in the GSP. However, my application needs to be able to set the number of columns (and their associated names) dynamically based on some database query results. The JSON format for this grid does not include the column names so I cannot use a g:each tag in my GSP to iterate through the names.
I wanted to do something like this but am unable to do so:
<dojo:grid controller='foo' action='getGridData' ...>
<g:each in=${columns}>
<dojo:col width="15%" name="{it}" field="{it}">{row.{it}}<dojo:col/>
</g:each>
</dojo:grid>
I gave the specific example of the Dojo plugin for background but I would like to extend this to a more general question about Grails. If I am constructing a GSP element by a controller action that renders JSON how do I access other things in the controller for the construction of nested elements? Is this even possible? I am still new to web development so I am sure I am misunderstanding some of the glue between these components.

The main thing I see wrong with your code is you're trying to reference the columns data incorrectly.
<g:each in="${columns}">
<dojo:col width="15%" name="${it}" field="${it}">{row.{it}}<dojo:col/>
</g:each>
Note the quotes around the in= value and the dollar sign used to reference the it variable.
Regarding the last bit
{row.{it}}
I'm unclear on what row is in this case. So I'm not sure how that needs to be fixed, but maybe this gets you moving in the right direction.
You generally don't render a GSP and construct JSON in one request. What will happen here is you render your GSP which takes all your taglib code and produces HTML/JavaScript. Then, once the browser renders it, an AJAX call takes place to fetch the JSON.
Remember that by the time the GSP reaches the browser, it is just HTML.

Related

How do I get Grails g:select with multiple-selection with all selections when returning from the controller

I have a page that is a report from a database and I'm working on modifying how the filtering works. The intention is to allow the user to select possible values form a list that will be used to filter the resulting report. There are too many values to do this with checkboxes. I'm defining a multiple selection list box with this:
<g:select name="country" from="${countryDataList.KOUNTRY}" value="${params.country}" multiple="true" />
countryDataList is a List<> of objects with a name and a value which I create in the controller. I'm able to get the selected counties and process them without an issue.
But when the page returns from the controller with the filtered report, only the first selection in the list is selected. It doesn't re-select all of the items that the user selected. I am passing the params.country object back from the controller as
country:params.country
I saw some posts about this not working, but they are all from several years ago. Am I missing a vital step?
Ahh sorry, I was reading it on the phone initially and missed the point.
So what you want is a way of sending a multiple select box to a confirmation page. If I understand correctly?
Anyways how many objects in the select are we talking massive or a dozen couple of dozen or so ?
What I did was use check boxes and did a confirmation which shows the selection ticked in check boxes.. So this is the confirmation page that loads in https://github.com/vahidhedayati/mailinglist/blob/master/grails-app/views/mailingListEmail/confirmcontact.gsp
this page which is where multiple attachments selected from the schedule re-appear...
https://github.com/vahidhedayati/mailinglist/blob/master/grails-app/views/mailingListAttachments/_mailerAttachmentsDisplay.gsp.
Please note advice below is all conceptual stuff and there may be easier ways than this
Other than that You could create a taglib call on the confirmation page https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/taglib/ajaxdependancyselection/AutoCompleteTagLib.groovy#L55 which takes in your arrayList you could probably convert it to JSON pass it into the javascript that you load in within the taglib (on mine further down it loads this page in)
https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/views/autoComplete/_selectJs1.gsp#L23
and look to reselect them using javascript... as I say I haven't tested the last bit, the first bit i.e. checkbox works it is/has been in use.
Years later from you I just had the same problem. What I figured out is: it happens when params.country is an array instead of a Collection (i.e. an ArrayList).
A workaround for this if you want to stick to the array type is at the value attribute of the tag doing this: params.country?.findAll().

multi line tag in grails or html

With a grails app and from a local database, I'm returning some text in a xml format.
I can return it well formed in a <textarea></textarea> tag with the correct indenting (tabulation, line return,...etc.)
I want to go a bit further. In the text I'm returning, there are some <img/> tags and I'd like to replace those tag by the real images themselves.
I searched around and found no solution as of now. I understood that you can't add an image to a textarea (other then in a background), and if I choose a div tag, I won't have the indenting anymore (and therefore, harder to read)
I was wondering if using a <g:textField/> or an other tag from the grails library will do the trick. And if so, How can I append them to a page using jquery.
For example, how to append a <g:textField/> in jquery. It doesn't interpret it and I get this error
SyntaxError: missing ) after argument list [Break On This Error]...+doc).append("<input type="text" id="FTMAP_"+nb_sec+"" ...
And in my javascript file, I have
$("#FTM_"+doc).append("<g:textField id='FTMAP_"+nb_sec+"' ... />
Any possible solutions ?
EDIT
I did forget to mention that my final intentions are to be able to modify the text (tags included) and to have a nice and neat indentation so that it is the easiest possible for the end user.
You are asking a few different questions:
1. Can I use a single HTML tag to include images inside pre-formatted text.
No. You will have to parse the text and translate it into styled text yourself.
2. Is there a tag in the grails standard tags to accomplish this for me?
No.
3. How can I add grails tags from my javascript code.
Grails tags are processed on the server-side, and javascript is processed on the client. This means you cannot directly add grails tags via javascript.
There are a couple methods that can accomplish the same result, however:
You can set a javascript variable to the rendered content of a grails tag. This solution is good for data that is known at the time of the initial request.
var tagOutput = "${g.textField(/* etc */)}";
You can make an ajax request for the content to be added. Then your server-side grails code can render the tags you need. This is better for realtime data, or data that will be updated more than once on a single rendered page.

using jquery templates in a loop, need to prefill input items with ViewBag.values

I have a jquery template that I'm looping over x number of times to repeat on a form. When that form is reloaded, it automatically runs the loop to display all of the inputs according to what was previously selected, but I can't for the life of me wrap my head around how to put the values into those fields.
using something like this:
(string)ViewBag.myFieldValue{id}
doesn't work because it thinks it's a c# object (obviously).
how can I accomplish this?
A jquery template needs a json object to render. So you should put the values from your viewbag into a json object. Even better, a template can also receive a json array, and will render x number of templates depending on the number of objects in the array.
Take a look at this blogarticle and you will figure it out soon:
An introduction to jquery templates

Struts2 & jquery - custom pagination - how to go to page2 without hitting the db again

thanks in advance for any input. I am using struts2 and jquery in this app.
I tried to use displaytag for pagination but my tables have images and there wasn't a way I could make displaytag work with images.
So now I have custom coded pagination which uses <s:subset> which works great so far except that I don't know how to make it go to another page.
Basically in <s:subset> I just want to change the start attribute and then refresh my JSP. My code evaluates the start attribute correctly with a given page number.
My s:subset tag is like below,
<s:subset source="pageableList.pagedList" count="pageableList.pageSize" start="pageableList.start" >
<s:iterator>
I think I want to use <s:url> to display my clickable page numbers but I'm having trouble there.
I have my page numbers in a list (which I evaluate in my action class right after my search completes), then in my JSP where I need to display the clickable page numbers, I iterate through the list, displaying the page numbers like below -
<s:iterator value="pageNumList" > | <a href='#'> <s:property/> </a> </s:iterator>
I guess I need to pass the clicked value of page number to the action class, then since the search results are in a list in the action class, without hitting the database again just display the results page with the new value of start attribute.
Any ideas how I might do that? I have been considering using a Decider attribute with <s:subset> but maybe there is a simpler way?
Thanks again for any input.
Regards,
veeCan
If you do not want to hit your DB again, cache the results of the initial query in RAM in your Action class (reference a static cache somewhere). Also, you don't have to go with a cache-all-or-nothing approach -- you can cache the first N pages and then when you near the end of the cache, fetch the rest. If you do it right, you can maintain minimum RAM footprint but preserve a snappy user experience that leverages user think time (depends on your app).

Grails/Groovy taglib handling parsing dynamically inserted tags

Is there a way to have a custom taglib operate on data loaded in a .gsp file such that it picks up any tags embedded in the data stored in the database. For instance, let's say I'm doing:
<g:each in="${activities}">
<li>${it.payload}</li>
</g:each>
And inside the payload, which is coming from the database, is text like
"Person a did event <company:event id="15124124">Event Description</company:event>"
Can you have a taglib that handles company:event tags on the fly?
You could write a custom tag which uses the GroovyPagesTemplateEngine to process the text and write it to the output stream. I think you can get an instance of the TemplateEngine injected into your tag from the applicationContext.
I don't have any example code sorry,
cheers
Lee

Resources