在将地图传递给 gsp 之前,尝试根据控制器中的一个字段对地图进行排序,并且它看起来在控制器中工作正常,但是 gsp 页面似乎以没有特定顺序的方式随机抓取项目。
控制器代码。尝试订购将显示的提示。
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>