我有一个与Note类一对多的Student类
class Student {
String name
static hasMany = [notes: Note]
}
class Note {
double note
int bimester
static belongsTo = [student:Student]
}
当我在userNotes.gsp中向用户显示结果时
它显示:
我使用:<g:each var="n" in="${studentInstance.notes}">
Discipline Note Bimester
mathematics | 0 | 1
mathematics | 0 | 2
mathematics | 0 | 3
mathematics | 0 | 4
portuguese | 0 | 1
portuguese | 0 | 2
portuguese | 0 | 3
portuguese | 0 | 4
但我想展示以下内容:
Discipline | Bimester 1 | Bimester 2 | Bimester 3 | Bimester 4
mathematics 0 0 0 0
portuguese 0 0 0 0
答案 0 :(得分:0)
您可以在控制器中对数据进行分组:
def userNotes() {
def student = Student.get(1)
def disciplines = [:].withDefault { [0, 0, 0, 0] }
for (Note note in student.notes.sort { it.bimester }) {
disciplines[note.discipline].set(note.bimester - 1, note.note)
}
[disciplines: disciplines]
}
并用
显示<table>
<thead>
<tr>
<th>Discipline</th>
<th>Bimester 1</th>
<th>Bimester 2</th>
<th>Bimester 3</th>
<th>Bimester 4</th>
</tr>
</thead>
<tbody>
<g:each in="${disciplines}" var="entry">
<tr>
<td>${entry.key}</td>
<g:each in="${entry.value}" var='bimester'>
<td>${bimester}</td>
</g:each>
</tr>
</g:each>
</tbody>
</table>