如何对 Grails 对象的 persistenceSet 进行排序?
我有两个域模型:
class Resource{
String name
static mapping = {
sort name:"asc"
}
}
class ResourceGroup{
String groupName
static hasMany = [resources: Resource]
}
控制器:
def resGroups = ResourceGroup.findAll()
render (
view: "index",
model: [resourcegroups: resGroups]
)
所以现在在我的 gsp 中:
<g:each in="${resourcegroups}" var="item" status="i">
...
<g:each in="${item.resources}" var="res" status="y">
<!-- THESE ITEM.RESOURCES ARE UNSORTED! -->
</g:each>
...
</g:each>
我的问题是如何对这个“item.resources”进行排序?这是hibernate的持久化设置!我认为这可以通过映射排序名称来处理:'asc',但它不起作用:-(
I have two domain models:
class Resource{
String name
static mapping = {
sort name:"asc"
}
}
class ResourceGroup{
String groupName
static hasMany = [resources: Resource]
}
controller:
def resGroups = ResourceGroup.findAll()
render (
view: "index",
model: [resourcegroups: resGroups]
)
so and now in my gsp:
<g:each in="${resourcegroups}" var="item" status="i">
...
<g:each in="${item.resources}" var="res" status="y">
<!-- THESE ITEM.RESOURCES ARE UNSORTED! -->
</g:each>
...
</g:each>
my Question is how can I sort this "item.resources"? this is a persistent set of hibernate! I thought this could be handled with the mapping sort name: 'asc', but it doesn't work :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
item.resources.sort { it.name }
替换为"asc"
,或
item.resources.sort { it.name }.reverse() 为
“desc”
。try
item.resources.sort { it.name }
for"asc"
,or
item.resources.sort { it.name }.reverse()
for"desc"
.您不能对一对多或多对多关系进行默认排序。请参阅 此处的文档,特别注意底部的注释:
您指定的默认排序实际上对
Resource
对象列表进行排序(即,如果您获得像这样的列表Resource.getAll()
,则列表将按以下顺序排序您指定的)。要执行您想要的操作,请考虑按照 Don 的建议创建一个选项卡库 此处。
You cannot have a default sort on a one-to-many or many-to-many relationship. See documentation here, paying particular attention to the note at the bottom that says:
The default sort you have specified actually does sort a list of
Resource
objects (i.e. if you got the list like thisResource.getAll()
the list would be sort in the order you specified).To do what you want consider creating a tab lib as Don suggests here.