I have two domains in a Grails application with a one-to-many relationship: Course and Person. One course may have many people. When I view show.gsp for Course, it will show a list of all person’s names who are registered for the Course.
I want that list to be sorted alphabetically by last name. However, each time that I refresh show.gsp, the order of the list changes (can’t seem to identify a pattern). I set up a mapping in my Person domain to sort by lastName “asc”, but that has no effect.
I had modified the scaffolded show.gsp to display the names in a table along with additional checkboxes. Here is that modified code:
<g:if test="${courseInstance?.persons}">
<br />
<table>
<thead>
<tr>
<th>#</th>
<g:sortableColumn property="person"
title="${message(code: 'person.lastName.label', default: 'Person')}" />
<g:sortableColumn property="paid"
title="${message(code: 'person.paid.label', default: 'Paid')}" />
<g:sortableColumn property="attended"
title="${message(code: 'person.attended.label', default: 'Attended')}" />
</tr>
</thead>
<tbody>
<g:set var="counter" value="${1}" />
<g:each in="${courseInstance.persons}" status="i" var="p">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td>
${counter}
</td>
<td class="property-value" aria-labelledby="persons-label"><g:link
controller="person" action="show" id="${p.id}">
${p?.encodeAsHTML()}
</g:link></td>
<td><g:checkBox name="paid" value="${p.paid}"
onclick="${remoteFunction(action:'togglePaid', id:p.id,
params:'\'completed=\' + this.checked')}" /></td>
<td><g:checkBox name="attended" value="${p.attended}"
onclick="${remoteFunction(action:'toggleAttended', id:p.id,
params:'\'completed=\' + this.checked')}" /></td>
</tr>
<g:set var="counter" value="${counter + 1}" />
</g:each>
</tbody>
</table>
</g:if>
Is there any modification I can make either in show.gsp or the domain to have the list sorted alphabetically by default (unless a sortable column is clicked)?
Thanks!
Could you show how you have it set on the Domain Level? It should like like so…
Check out the documentation here. If the sorting does seem to be correct but still not working you could try stopping the app and doing a
grails cleanand re-running it.UPDATE:
answer is the comments below…