使用以下代码:
<g:each in="${books.sort{it.date}}" status="i" var="book">
${book}
</g:each>
我想显示按日期排序的书籍;另外,我希望当前登录的人创作的书首先出现。
这可能吗?
答案 0 :(得分:1)
不确定要实现的目标,但您可以这样做。在将books
列表传递给GSP之前,您可以这样写:
def myAction() {
List books = Book.list() // Get your book list
User loggedInUser = User.first() // Get your currently logged in user
// First get all the books of current user sorted by the date
List currentUsersBooks = books.findAll { it.author.id == loggedInUser.id }.sort{ it.date }
// Then get other books sorted by date
List otherBooks = books.findAll { it.author.id != loggedInUser.id }.sort{ it.date }
// Now merge all of them (as `List` will maintain insertion order)
// So current user's book will be listed first and then others
List allBooks = currentUsersBooks + otherBooks
[books: allBooks]
}
现在,修改您的GSP,不要再次排序:
<g:each in="${books}" status="i" var="book">
${book}
</g:each>
考虑到Book
和User
域类,如下所示:
class User {
String email
}
class Book {
Strig title
User author
}