idonaldson
idonaldson

Reputation: 475

Sorting in grails map not showing up in gsp

Trying to sort a map based on one field in a controller before it is passed to the gsp, and it looks to be working fine in the controller, however the gsp page seems to randomly grab the items in no specific order.

Controller code. Trying to order the prompts that will be displayed.

 def show(Long id) {
    def reportInstance = Report.get(id)
    reportInstance.prompts = reportInstance.prompts.sort{it.displaySequence}
    [reportInstance: reportInstance]
}

If I put that in a println statement, it shows it sorted in the console.

Domain object that it is working with:

class Report {

    Long id
    String name
    String department
    String description
    String summary
    Date activityDate

    static hasMany = [prompts:Prompt]

    static mapping = {
            sort "id"
           version false
           table 'reports'
           columns{
                   id column: 'id'
                   name column: 'name'
                   department column: 'department'
                   description column: 'description'
                   summary column: 'summary'
                   activityDate column: 'activity_date'
           }
           id generator:'sequence', params:[sequence:'reports_id_sequence']
    }

    static constraints = {
           name(nullable: false, maxSize: 60)
           department(nullable: false, maxSize: 60)
           description(nullable: true, maxSize: 120)
           summary(nullable: true, maxSize: 500)
           activityDate(nullable: false)

    }

    String toString() {
        "${name}"
    }

Here is the snippet from the gsp page in question.

<g:if test="${reportInstance?.prompts}">
    <li class="fieldcontain">
        <h3 class="property-label">Prompts</h3>
        <br>
        <table id="prompts">
            <thead>
                <tr>
                    <th>${message(code: 'prompt.name.label', default: 'Name')}</th>
                    <th>${message(code: 'prompt.description.label', default: 'Description')}</th>
                    <th>${message(code: 'prompt.required.label', default: 'Required')}</th>
                    <th>${message(code: 'prompt.displaySeqno.label', default: 'Display Order')}</th>
                </tr>
            </thead>
            <tbody>
                <g:each in="${reportInstance.prompts}" status="i" var="prompt">
                    <tr class="${(i % 2) == 0 ? 'even' : 'odd'}" onclick='window.location = "${createLink(controller: "prompt", action: "edit", id: prompt.id)}"'>
                        <td>${fieldValue(bean: prompt, field: "name")}</td>
                        <td>${fieldValue(bean: prompt, field: "description")}</td>
                        <td>${prompt.required}</td>
                        <td class="displaySeqno">${prompt.displaySeqno}</td>
                    </tr>
                </g:each>
            </tbody>
        </table>
    </li>
</g:if>

Upvotes: 0

Views: 2124

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

In your Report domain class prompts is a Set, not a List, so you can't sort it like that. You'll need to pass the sorted list separately in the model:

def show(Long id) {
    def reportInstance = Report.get(id)
    [reportInstance: reportInstance,
     prompts:reportInstance.prompts.sort{it.displaySequence}]
}

and use that in the GSP

<g:each in="${prompts}" status="i" var="prompt">

or just pass the reportInstance and do the sorting in the GSP

<g:each in="${reportInstance.prompts.sort{it.displaySequence}}"
    status="i" var="prompt">

Upvotes: 1

Related Questions