如何在Grails gsp列表中显示结果

如何在Grails gsp列表中显示结果,grails,gsp,Grails,Gsp,我有一个学生班,它与笔记班有一对多的关系 class Student { String name static hasMany = [notes: Note] } class Note { double note int bimester static belongsTo = [student:Student] } 当我在我的userNotes.gsp中向用户显示结果时 如图所示: 我使用: 但我想展示以下几点: Discipline | Bimester

我有一个学生班,它与笔记班有一对多的关系

class Student {
   String name
   static hasMany = [notes: Note]
}

class Note {
    double note
    int bimester
    static belongsTo = [student:Student]
}
当我在我的userNotes.gsp中向用户显示结果时 如图所示: 我使用:

但我想展示以下几点:

Discipline | Bimester 1 | Bimester 2 | Bimester 3 | Bimester 4
mathematics      0            0            0            0
portuguese       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>

我建议您研究findAll标记,正如这里所解释的,并找出针对您的特定问题的最佳解决方案:我的最佳选择是将其与if标记结合使用:
<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>