English 中文(简体)
Grails - Sorting list production without had a SortedSet or Comparativeable model?
原标题:Grails - Sorting list output without having to have a SortedSet or Comparable model?

我禁止我站在墙上,谈谈我认为在铁路解决的一个非常简单的问题:

我说,我有像购物一样的模式;因此,Cart语有管理物品,每个物品都属于其中。 To the cart. 总的来说,我不关心车上物品的顺序——我不关心它们储存、计算等等的顺序。 HOWEVER,我确实希望按同样顺序排列。 在我看来,这种逻辑应当能够存在于观点层面,但我能找到的唯一解决办法,告诉我,在模型层把物品宣布为SortedSet。 这还影响到我的控制层,因为像集体{}这样的简单清单操作现在需要额外的合成跳跃,以保持类型转换的正确性和维护我的分类。

对我来说,这只是一个nut子,因此我必须失踪! 例如,有没有办法做如下事情:<g:each in=”${cart.itemssort{it.code>>或其他类似内容,以便我能够在产出/观点层面强制执行一个连续的显示单? EDIT - 参见下文的主题说明;其中的版本确实有效。

感谢您提出任何建议或要点。

最佳回答

这3个政党与你一样需要做些什么。 否则,你就能够永远打下主角。 塔格人会这样做。

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

之后,这些标签可以用来按其名称财产对物体进行分类:

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

由<代码>某些孔波图查询的收集工作,将在标本交付时进行分类。

问题回答

http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html

我本人发现,与<g:each/>一起这样做非常容易。 我的普惠制标签:

<!-- Books sorted by title -->
<g:each in="${ books.sort{a,b-> a.title.compareTo(b.title)} }">
    <p>Title: ${it.title}</p>
    <p>Author: ${it.author}</p>
</g:each>

关于你能够操纵收集和地图的更多方式,我建议





相关问题
Groovy - how to exit each loop?

I m new to Grails/Groovy and am trying to find a node in a an xml file; I ve figured out how to iterate over all of them, but I want to exit the loop when the target node is found. I ve read that ...

Eclipse Spring Builder set properties with Groovy beans

I typically use groovy to construct simple bean but the Spring IDE plugin to eclipse fails to build when I try to set a property that is generated by groovy without an explicit setter. For example, ...

How can I get this snippet to work?

I d like to port a little piece of code from Ruby to Groovy, and I m stuck at this: def given(array,closure) { closure.delegate = array closure() } given([1,2,3,4]) { findAll { it > 4} ...

Changing the value in a map in Groovy

This is about a very basic program I m writing in Groovy. I have defined a map inside a method: def addItem() { print("Enter the item name: ") def itemName = reader.readLine() print(...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...