我有两个域模型:
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]
)
所以现在在我的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”进行排序?这是一个持久的休眠状态!我以为可以用映射的排序名称'asc'来处理,但这不起作用:-(
最佳答案
您不能对一对多或多对多关系进行默认排序。请参阅documentation here,特别注意底部的注释:
These mappings will not work for default unidirectional one-to-many or many-to-many relationships because they involve a join table. See this issue for more details. Consider using a SortedSet or queries with sort parameters to fetch the data you need.
您指定的默认排序实际上会对
Resource
对象列表进行排序(即,如果您获得像Resource.getAll()
这样的列表,则列表将按照您指定的顺序进行排序)。要执行您想做的事情,请考虑创建一个标签库,如Don所建议的here。
关于sorting - 如何对grails对象的persistentSet进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8439433/