Background: The g:sortableColumn tag by default invokes the list action in a controller, passing it a “sort” parameter, which indicates which field to sort by. In my specific case, I have an Issue class/list that I’m sorting:
class Issue {
String title
...
User assignedTo
...
}
and
Class User {
String lastName
...
}
Specifying a g:sortableColumn property=”assignedTo.lastName”, which translates into the sort parameter of the list, works great if the issue has been assigned — i.e. assignedTo is non null. But if it hasn’t, then list() no longer returns it. I don’t know if this is a fault of list() or not (comments?), but I’d like to have those Issues with a null assignedTo come at the end.
I thought about combining Issue.list() with an Issue.findAllByAssignedToIsNull(), but it will require coordinating max/offset in both, which I’d rather avoid if possible.
Thoughts/comments?
——- per comment ——
It is the standard (Grails 1.3.7) scaffolded list action, i.e.:
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[issueInstanceList: Issue.list(params), issueInstanceTotal: Issue.count()]
}
—– per second comment (mine) —————————————-
I also tried the following, once again it did not return the Issues where assignedTo was null:
<g:sortableColumn property="assignedTo.lastName" title="${message(code: 'issue.assignedTo.label', default: 'Assigned To')}" />
def list = {
if (params.sort == 'assignedTo.lastName') {
println "doing special find/sort by: ${params.sort}"
return [issueInstanceList: Issue.findAllByAssignedToIsNullOrAssignedToIsNotNull(params), issueInstanceTotal: Issue.count()]
}
[issueInstanceList: Issue.list(params), issueInstanceTotal: Issue.count()]
}
You have to force a left join on the user table. Here’s one way to do it with a criteria query:
Passing
paramsinto the criterialist()method automatically handles pagination and sorting, just like the domain objectlist()method. OurcreateAliashappens too late though, so we have to removesortandorder, then reapply them manually after creating an alias forcing the left join.