gpt4 book ai didi

grails - Grails中的两个排序规则

转载 作者:行者123 更新时间:2023-12-02 14:41:11 24 4
gpt4 key购买 nike

使用以下代码:

<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>

考虑到 BookUser域类是这样的:
class User {
String email
}

class Book {
Strig title
User author
}

关于grails - Grails中的两个排序规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34127508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com