在grails中排序没有显示在gsp中

时间:2013-02-14 21:22:33

标签: grails groovy

尝试在将映射传递给gsp之前基于控制器中的一个字段对映射进行排序,并且它看起来在控制器中正常工作,但gsp页面似乎随机抓取项目没有特定的顺序。 / p>

控制器代码。尝试订购将要显示的提示。

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

如果我把它放在println语句中,它会在控制台中显示它。

它正在使用的域对象:

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}"
    }

以下是相关gsp页面的摘录。

<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>

1 个答案:

答案 0 :(得分:1)

在您的Report域名prompts中,SetList,而不是def show(Long id) { def reportInstance = Report.get(id) [reportInstance: reportInstance, prompts:reportInstance.prompts.sort{it.displaySequence}] } ,因此您无法对其进行排序。您需要在模型中单独传递已排序的列表:

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

并在GSP中使用

reportInstance

或只是传递<g:each in="${reportInstance.prompts.sort{it.displaySequence}}" status="i" var="prompt"> 并在GSP中进行排序

{{1}}