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

标签 grails gsp

我有一个与笔记类一对多关系的学生类

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

最佳答案

您可以在 Controller 中对数据进行分组:

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>

关于grails - 如何在Grails gsp列表中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30170029/

相关文章:

grails - Grails:g:each标签中的GSP变量替换

grails - 无法访问已加载模板中的域类方法

ajax - 在grails中将字符串返回给ajax调用

ajax - Grails remoteLink:整个页面已加载以响应

Grails 多个数据源: org. springframework.beans.factory.NoUniqueBeanDefinitionException

ajax - Grails 刷新 div

grails - 如何使用 apache POI 逐行遍历 .docx 文件

grails - g:选择值在填充选择后消失

grails - 从Grails中的hasMany中仅获取ID?

grails - 适用于 Grails 3.x 的 Gradle DSL