Today I needed to knock up a quick interface to some database tables for inputting and editing data to be used in the demonstration of some data extraction software. Aha I thought; I’ll try using Grails Scaffolding. I haven’t used grails scaffolding in earnest since taking a grails training course few years back. However, today I really did just need some simple CRUD functionality.
In Grails 3, you have two options for scaffolding – annotating the controller and having the views and controller methods auto-generated at runtime, or running a command to generate them statically so that you can modify them. I chose the latter, assuming that I’d want to do some customisation.
grails generate-all com.domain.Thing
You can then inspect the generated controller and views, and make any changes necessary. And this is where it all started to go wrong. The table containing the list of existing records didn’t look very nice. I’d removed the default ‘application.css’ which comes with grails, and used bootstrap to style the app. Without the default styles, the table has no spacing, and looks pretty awful.
No problem, I just need to add class=”table” to the table and I’ll get a standard bootstrap styled table. However, the generated index.gsp doesn’t contain a table tag. All I found was this:
<f:table collection="${thingList}"/>
The <f:table/> tag was a new one to me. Google suggests this comes from the grails fields plugin, but the documentation is very sparse: Grails 3 fields plugin.
The documentation doesn’t even mention the <f:table/>http://grails3-plugins.github.io/fields/snapshot/ref/Tags/table.html which helped a bit, in that it showed how to configure which fields to show in the table but didn’t help in changing styles or other formatting.
The main grails scaffolding documentation suggests running
grails install-templates
to get local copies of the templates used in scaffolding, but this doesn’t include anything to do with the fields plugin.
More detective work led to this Stackoverflow post, and onward to the fields plugin source code.
Finally… how to customise the f:table tag:
Place a file called _table.gsp in /grails-app/views/templates/_fields/
The default file contents are here: _table.gsp
After adding this file to the project and amending to use the required styles, the <f:table/> tag can be used throughout the project with reckless abandon.
My table looks nice now, but I think this sums up why I struggle with the grails plugin ecosystem; it feels a bit half-finished to be using an undocumented tag as part of what should be a quick start process for new users.
Thanks. Google: “grails f:table empty column” led me here.
The _table.gsp file is the place I needed to go to add an “edit” link to the listing of a domain class that happened to have a null value in the first column.
Cheers!
This is my pet peeve with Grails. Too many changes without backing documentation. Thank you so much for this article!