Grails,使用 findAllByxx 获取索引列表时分页不起作用

Grails, paging doesn't work when using findAllByxx to get index-list

当我查看 OfferHeader 的 index.gsp 时,我只看到 "Previous" 按钮,当点击它直到找到顶部时,按钮消失了。 问题是我需要使用 findAllBy-list 来获取过滤后的列表,因此响应可能如下所示:

respond offerHeader, model:[offerHeaderCount: offerHeader.count()]

因为 offerHeader.count() 是不允许的,所以我改用了 .size()。

控制器索引如下所示:

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    def offerHeader = OfferHeader.findAllByOfferType('o',params)
    def count = offerHeader.size()
    respond offerHeader, model:[offerHeaderCount: offerHeader.size()]
}

修正案

嗯 Grails 不喜欢我..

我收到这个错误:

URI
    /offerHeader/index
Class
    groovy.lang.MissingPropertyException
Message
    null
Caused by
    No such property: createCriteria for class: com.torntrading.portal.OfferHeader

Around line 15 of grails-app\controllers\com\torntrading\portal\OfferHeaderController.groovy

12:
13:    def index(Integer max) {
14:        params.max = Math.min(max ?: 10, 100)
15:        def offerHeader = OfferHeader.createCriteria.list( params ) { eq ( "offerType", "o" )}
16:
17:        def count = offerHeader.size()
18:        respond offerHeader, model:[offerHeaderCount: offerHeader.totalCount

编辑

creteCriteria 应该这样使用:

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        def offerHeader = OfferHeader.createCriteria().list( params ) { eq ( "offerType", "o" )}

        def count = offerHeader.size()
        respond offerHeader, model:[offerHeaderCount: offerHeader.totalCount]

    }

条件查询在这种情况下可能更好:

def offers = OfferHeader.createCriteria().list( params ) {
    eq ( "offerType", "o" )
}

respond offers, model: [offerHeaderCount: offers.totalCount]