Grails - 无需使用 SortedSet 或 Comparable 模型即可对列表输出进行排序? [英] Grails - Sorting list output without having to have a SortedSet or Comparable model?

查看:23
本文介绍了Grails - 无需使用 SortedSet 或 Comparable 模型即可对列表输出进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我认为在 Grails 中可以解决的一个非常简单的问题,我感到很头疼:

I am banging my head up against the wall about what I think would be a very simple problem to resolve in Grails:

说我有类似购物车的模型;所以一个类 Cart 有很多项目,每个项目都属于购物车.一般来说,我不关心购物车中物品的顺序 - 我不关心它们的存储顺序、计算顺序等.但是,我确实希望以相同的顺序显示它们.在我看来,这个逻辑应该能够完全存在于视图层中,但我能找到的唯一解决方案告诉我在模型层中将项目声明为 SortedSet.这也会影响我的控制器层,因为像 .collect{} 这样的简单 List 操作现在需要额外的语法跳转,以保持类型转换正确并保留我的排序.

Say that I have shopping-cart-like model; so a class Cart that hasMany items, and each item belongsTo the cart. In general, I don't care about the order of the items in the cart - I don't care what order they're stored in, calculated in, etc. HOWEVER, I do want to DISPLAY them in the same order. It seems to me that this logic should be able to exist ENTIRELY in the view layer, but the only solutions I've been able to find tell me to declare items as a SortedSet in the model layer. This also affects my controller layer, as simple List operations such as .collect{} now require extra syntactic jumping around to keep the type conversions correct and preserve my sorting.

对我来说,这太疯狂了,所以我一定遗漏了一些简单的东西!例如,有什么方法可以做类似 <g:each in="${cart.items.sort{it.name}}"> 或类似的事情,以便我只能在输出/视图层强制执行一致的显示顺序吗?编辑 - 请参阅下面的马特回答;这个版本确实有效.

To me, this is nuts, so I must be missing something simple! Is there any way, for example, to do something like <g:each in="${cart.items.sort{it.name}}"> or something similar, so that I can enforce a consistent display order ONLY at the output / view layer? EDIT - See Matt's answer below; a version of this does actually work.

感谢您的任何建议或指点!

Thank you for any advice or pointers!

推荐答案

这个 3rd party标签 看起来它会做你需要的.如果没有,您可以随时制作自己的标签.Tag 类可以像这样进行排序

This 3rd party tag looks like it will do what you need. If not, you can always make your own tag. The Tag class could do the sorting like this

class SortTagLib {

    static namespace = 'sort'

    def sort = { attrs ->

        // A closure that does the sorting can be passed as an attribute to the tag.
        // If it is not provided the default sort order is used instead
        def sorter = attrs.sorter ?: {item1, item2 -> item1 <=> item2}
        sorter = sorter as Comparator        

        // The collection to be sorted should be passed into the tag as a parameter
        Collections.sort(attrs.items, sorter)
    }
}

然后可以使用此标记按名称属性对对象集合进行排序,如下所示:

This tag could then be used to sort a collection of objects by their name property like this:

<sort:sort items="someCollection" sorter="${someComparatorClosure}"/>

someCollection 引用的集合将在标签执行时就地排序.

The collection referred to by someCollection will be sorted in-place when the tag is executed.

这篇关于Grails - 无需使用 SortedSet 或 Comparable 模型即可对列表输出进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆