Grails Sorting给我带来困难

时间:2014-11-27 13:05:00

标签: sorting grails

新的Grails排序!我目前正在处理grails 2.3.11,我无法理解为什么以下代码无法按预期工作!

def listCommodity() {
        List<Commodity> commodities;

        if (params.searchBox) {
            commodities = Commodity.list().findAll {
                it.name.toLowerCase().contains("${params.searchBox.toLowerCase()}") ||
                        it.type.name.toLowerCase().contains("${params.searchBox.toLowerCase()}") ||
                        (it.type.parent ? it.type.parent.name.toLowerCase().contains("${params.searchBox.toLowerCase()}") : false)
            }

        } else {
            commodities = Commodity.list()
        }

        commodities?.sort { one, two ->
            if ("decs" == params?.order) {
                return two.name <=> one.name
            } else {
                return one.name <=> two.name
            }
        }

        def max = Math.min((params.max ?: 10) as Long, 100)
        def offset = Math.min((params.offset ?: 0) as Long, commodities.size() - 1)

        if (!commodities) {
            flash.message = "No Items found Here!"
            return [commodities: [], commoditiesCount: 0]
        } else {
            return [commodities: commodities[offset..Math.min(offset + max, commodities.size() - 1)], commoditiesCount: commodities.size(), searchBoxText: params.searchBox ?: '']
        }
    }

观点:

....
            <tr>
                <g:sortableColumn params="${[searchBox:searchBoxText]}" property="name" title="${message(code: 'commodity.commodity.name.label', default: 'Name')}" />
            </tr>
....
            <% println commodities //here it prints the items in ascending %>
            <g:each in="${commodities}" status="i"  var="com">
                <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                    <td>
                        ${com?.getName(lang)}
                    </td>
                </tr>
            </g:each>
....

此测试通过:

    given:
        mockDomain(CommodityType,[[name: "Cereals"],[name: "Group", parent: new CommodityType(name: 'Corea')]])
        mockDomain(Commodity,
                [[name:"Kea", type:CommodityType.findByName("Cereals")],
                 [name:"Shiro", type:CommodityType.findByName("Cereals")],
                 [name:"Other with ea", type:new CommodityType(name: "Grass")],
                 [name:"Barely", type:CommodityType.findByName("Group")],
                 [name:"Teff",type:CommodityType.findByName("Cereals")]])
    when:
        params.order = "decs"
        params.searchBox = "ea"
        def models = controller.listCommodity()

    then:
        5 == models.commodities.size()
        "Barely" == models.commodities[-1].name
        "Teff" == models.commodities[0].name
        "ea" == models.searchBoxText

    when:
        params.order = "asc"
        params.searchBox = "ea"
        def models2 = controller.listCommodity()

    then:
        5 == models2.commodities.size()
        "Barely" == models2.commodities[0].name
        "Teff" == models2.commodities[-1].name
        "ea" == models2.searchBoxText

但是当我在浏览器上测试它时!排序没有做任何事情!我在视图上做了一个println,列表从未排序!我做错了什么?

1 个答案:

答案 0 :(得分:2)

要检查两件事:

  1. 在第一个参数中使用sort(boolean, closure)和true对列表本身进行排序,否则创建副本
  2. 标准参数是&#34; desc&#34;不是&#34; decs&#34;
  3. 祝你好运!