0
我有一个脚手架Grails应用程序,具有两个域Person和Course。人属于课程,课程有许多人。我修改了show.gsp,以列出与选定课程关联的所有人员。Grails在关联列表中列出重复的姓氏
我遇到的问题是,显示给定课程中所有人的列表不显示人员在数据库中有重复的姓氏。举例来说,如果我有4人:“张三”,“李四”,“乔·多伊”,“爱德华·史密斯”,那么列表将只显示:
- 李四
- 爱德华·史密斯
但是,所有4人都在数据库中。另外,/ person/list将显示所有的名字。所以问题只出现在与课程相关的人员名单上。请参阅下面的相关代码。有什么想法吗?
人域:
class Person implements Comparable {
static mapping = { sort lastName: "asc" }
// Needed to sort association in Course domain (due to Grails bug)
int compareTo(obj) {
lastName.compareToIgnoreCase(obj.lastName);
}
String firstName
String lastName
String email
Course course
static belongsTo = [ course:Course ]
static constraints = {
firstName size: 1..50, blank: false
lastName size: 1..50, blank: false
email email: true
course()
firstName(unique: ['lastName', 'email'])
}
String toString() {
return this.lastName + ", " + this.firstName;
}
}
场域:
class Course {
int maxAttendance
SortedSet persons
static hasMany = [ persons:Person ]
static mapping = {
persons cascade:"all-delete-orphan"
}
def getExpandablePersonList() {
return LazyList.decorate(persons,FactoryUtils.instantiateFactory(Person.class))
}
static constraints = {
maxAttendance size: 1..3, blank: false
}
}
用于课程改性show.gsp:
<g:if test="${courseInstance?.persons}">
<br />
<table>
<thead>
<tr>
<th>#</th>
<g:sortableColumn property="person"
title="${message(code: 'person.lastName.label', default: 'Person')}" />
</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()}</td>
</tr>
<g:set var="counter" value="${counter + 1}" />
</g:each>
</tbody>
</table>
</g:if>
非常棒,谢谢。你会推荐什么作为最好的解决方法?我遇到了许多问题,正确排序关联,因此我目前的实施。 – littleK 2013-02-17 20:05:57
@littleK我不知道说实话。在我的应用程序中,我总是在排序时使用普通Set(如果需要在gsp中以特定顺序呈现结果,则在代码中进行排序),或者在执行时使用List。 – 2013-02-17 20:17:44
无论如何。虽然我知道这对性能不利,但我只是要在show.gsp中对列表进行排序。我想不出任何其他方式! \t \t \t \t \t \t a.lastName.compareToIgnoreCase(b.lastName)}}”status =“i”var =“p”> –
littleK
2013-02-17 20:46:55