Suppose I have two domain objects, Document and Author
Class Document {
Author author
String title
}
Class Author {
String lastName
String firstName
String toString() {
return lastName + ", " + firstName
}
}
The view list.gsp looks something like this:
<g:sortableColumn property="title" title=... />
<g:sortableCOlumn property="author" title=... />
....
<td>${fieldValue(bean: documentInstance, field: "author"}></td>
<td>${fieldValue(bean: documentInstance, field: "title"}></td>
The displayed values in the table work as intended – the table row will show the author as (lastName, firstName) next to documentInstance.title.
However, clicking the Author column header to sort causes “documents” to be sorted by author.id .
What is the most expedient way to get sorting by author.toString() or “author.lastName, author.firstName” instead of sorting by author.id?
I’d prefer to avoid falling back to .withCriteria{} if possible – I have four different columns that need this functionality, and it seems like that would get messy.
I’m just a Grails beginner so maybe my answer is not optimal, but this was the easiest way for me:
Instead of using
Document.list(params)I usedDocument.findAll(). Is worth mentioning that in my application I do needed some kind of filter in my lists, sofindAll()was the best approach. Anyways, here is how I would do it:And in the View: