0

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.

Jeff White
  • 43
  • 7

1 Answers1

1

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.

Gregg
  • 34,973
  • 19
  • 109
  • 214
  • Thanks for the feedback Gregg. I am still trying to get my head around the flow of control with these web frameworks. As you can tell I am still learning the syntax :) I ended up going back to my original idea of using DataTables and was able to get that to work. Originally when I added the Grails navigation plugin I messed up the flow of control and the app wasn't triggering my getJSON function anymore. – Jeff White Oct 26 '12 at 00:59