Wednesday, June 6, 2012

Auto nested property extraction...

I ran into the need for a Grails supported select box with 'optgroup'. I was suprised to find out Grails didn't support this, but I did find a JIRA ticket where a guy added the code but it appears it was forgotten and never included. Here's the link with the code: However I found the example:

<g:selectWithOptGroup name = "" 
                      from = "${Song.list()}" 
                      optionKey = "id" 
                      optionValue = "songName" 
                      groupBy = "album" />

groupBy doesn't support a tested property I'd expect. For example, say album is a parent 
property domain class, and you really want it to group by the parent's property of ''.
It will blow up when you do this. So I've modified this bit of code, learning from the Groovy class 'Eval'.

I added this around line 40 of the MyAppTagLib.groovy file:

 if (groupBy instanceof String && groupBy.indexOf(".")) {

                groupKey = Eval.x(el,"x."+groupBy)
This will expand a dot notated nested property to give you the correct string value to sort on. Hopefully this will be merged in the Grails core soon, it seems like people expect this sort of thing these days.

No comments: