YSL
YSL

Reputation: 22

Grails GSP Formatting Issues

I am trying out Grails, and am encountering some problems with GSP.

I have three classes, namely: Author, Books and Award. 1 author can have many books, and each book can have multiple awards. The following are the domain controllers:

class Author {

    String  authorName
    static  hasMany = [books : Book]

}

class Book {

    String      bookName
    BigDecimal  price

    static      belongsTo=[author : Author]
    static      hasMany=[awards: Award]
}

class Award {

    String  awardName
    Date    awardDate

    static  belongsTo = [book : Book]

}

The following is my GSP:

        <table>
            <th >Author Name</th>
            <th >Books</th>
            <th >Awards</th>

            <g:each in="${authorList}" var="author" status="i">
                <tr>

                    <td> ${author.authorName} </td>
                    <td> ${author.books} </td>
                    <td> ${author.books.awards} </td>
                </tr>
            </g:each>
        </table>

It is currently showing:

Books as: [Book 2, Book 1]
Awards as: [[], [Award 1]]

I would like to show them as: Books: 1. Book 2 2. Book 1

Awards:
1.
2. Award 1

Can I put both Book and Award into an object and use to iterate them?

Thank you in advance.

Upvotes: 0

Views: 56

Answers (1)

CHAAAA
CHAAAA

Reputation: 46

${author.books} and ${author.books.awards} is a SET, so you can

<g:each in="${author.books}" var="book">
    <tr>
        <td> ${book?.bookName} </td>
        <td> ${book?.price} </td>
    </tr>
</g:each>

Upvotes: 1

Related Questions