使用以下代码:
<g:each in="${books.sort{it.date}}" status="i" var="book">
${book}
</g:each>
我想显示按日期排序的书;另外,我希望首先显示当前登录者创作的书籍。
那可能吗?
最佳答案
不确定要达到的目标,但是可以做到这一点。在将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
}
关于grails - Grails中的两个排序规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34127508/