Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails sorting in a one-to-many relationship
    text
    copied!<p>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.</p> <p>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.</p> <p>I had modified the scaffolded show.gsp to display the names in a table along with additional checkboxes. Here is that modified code:</p> <pre><code>&lt;g:if test="${courseInstance?.persons}"&gt; &lt;br /&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;#&lt;/th&gt; &lt;g:sortableColumn property="person" title="${message(code: 'person.lastName.label', default: 'Person')}" /&gt; &lt;g:sortableColumn property="paid" title="${message(code: 'person.paid.label', default: 'Paid')}" /&gt; &lt;g:sortableColumn property="attended" title="${message(code: 'person.attended.label', default: 'Attended')}" /&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;g:set var="counter" value="${1}" /&gt; &lt;g:each in="${courseInstance.persons}" status="i" var="p"&gt; &lt;tr class="${(i % 2) == 0 ? 'even' : 'odd'}"&gt; &lt;td&gt; ${counter} &lt;/td&gt; &lt;td class="property-value" aria-labelledby="persons-label"&gt;&lt;g:link controller="person" action="show" id="${p.id}"&gt; ${p?.encodeAsHTML()} &lt;/g:link&gt;&lt;/td&gt; &lt;td&gt;&lt;g:checkBox name="paid" value="${p.paid}" onclick="${remoteFunction(action:'togglePaid', id:p.id, params:'\'completed=\' + this.checked')}" /&gt;&lt;/td&gt; &lt;td&gt;&lt;g:checkBox name="attended" value="${p.attended}" onclick="${remoteFunction(action:'toggleAttended', id:p.id, params:'\'completed=\' + this.checked')}" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;g:set var="counter" value="${counter + 1}" /&gt; &lt;/g:each&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/g:if&gt; </code></pre> <p>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)?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload