gpt4 book ai didi

grails - 带有多个字段的Grails sortableColumn

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

假设我有两个域对象,文档和作者

Class Document {
Author author
String title
}

Class Author {

String lastName
String firstName

String toString() {

return lastName + ", " + firstName
}
}

View list.gsp看起来像这样:
<g:sortableColumn property="title" title=... />
<g:sortableCOlumn property="author" title=... />

....

<td>${fieldValue(bean: documentInstance, field: "author"}></td>
<td>${fieldValue(bean: documentInstance, field: "title"}></td>

表中显示的值按预期工作-表行将在documentInstance.title旁边将作者显示为(lastName,firstName)。

但是,单击Author列标题进行排序会导致authors.id对“文档”进行排序。

什么是通过author.toString()或“author.lastName,author.firstName”而不是author.id进行排序的最便捷方法?

如果可能的话,我宁愿避免使用.withCriteria {}-我有四个不同的列需要此功能,而且看起来会很混乱。

最佳答案

我只是Grails的初学者,所以也许我的答案不是最佳的,但这对我来说是最简单的方法:

我没有使用Document.list(params),而是使用Document.findAll()。值得一提的是,在我的应用程序中,我确实需要列表中的某种过滤器,因此findAll()是最好的方法。无论如何,这是我的做法:

Document.findAll( "from Document as d order by d." + params.sort + ' ' + params.order, params ) //supports pagination

并在 View 中:
<g:sortableCOlumn property="author.lastName" title=... />

关于grails - 带有多个字段的Grails sortableColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10163438/

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