grails2.2中的分页为什么我不能把查询和合计总记录数放到一段代码中?

oksonic 2013-01-11

 

def searchClosure = {
            if (params) {
                eq('status', PostStatus.VERIFIED_SUCCESS);
                // 按标签查询
                if (params.tags) {
                    eq('tags', params.tags);
                }
                if (params.keyword){
                    or {
                        like("title","%${params.keyword}%")
                        like("brief","%${params.keyword}%")
                    }
                }
                if (params.postTime) {
                    DateTime beginDate = new DateTime(params.postTime + '-01');
                    DateTime endDate = beginDate.plus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay)
                    ge("postTime", DateUtil.stringToDate(beginDate.toString()))
                    le("postTime", DateUtil.stringToDate(endDate.toString()))
                }
            }
            maxResults(params.max?params.max.toInteger():paramsService.pageMax)
            firstResult(params.max?params.offset.toInteger():0)
            order("id", "desc")
        }
主要是加了这一句firstResult(params.max?params.offset.toInteger():0)后无法统计总记录数

 

        def searchClosure = {
            if (params) {
                eq('status', PostStatus.VERIFIED_SUCCESS);
                // 按标签查询
                if (params.tags) {
                    eq('tags', params.tags);
                }
                if (params.keyword){
                    or {
                        like("title","%${params.keyword}%")
                        like("brief","%${params.keyword}%")
                    }
                }
                if (params.postTime) {
                    DateTime beginDate = new DateTime(params.postTime + '-01');
                    DateTime endDate = beginDate.plus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay)
                    ge("postTime", DateUtil.stringToDate(beginDate.toString()))
                    le("postTime", DateUtil.stringToDate(endDate.toString()))
                }
            }
            maxResults(params.max?params.max.toInteger():paramsService.pageMax)
            firstResult(params.max?params.offset.toInteger():0)
            order("id", "desc")
        }
        def searchClosure1 = {
            if (params) {
                eq('status', PostStatus.VERIFIED_SUCCESS);
                // 按标签查询
                if (params.tags) {
                    eq('tags', params.tags);
                }
                if (params.keyword){
                    or {
                        like("title","%${params.keyword}%")
                        like("brief","%${params.keyword}%")
                    }
                }
                if (params.postTime) {
                    DateTime beginDate = new DateTime(params.postTime + '-01');
                    DateTime endDate = beginDate.plus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay)
                    ge("postTime", DateUtil.stringToDate(beginDate.toString()))
                    le("postTime", DateUtil.stringToDate(endDate.toString()))
                }
            }
        }
        def c = Post.createCriteria()
        def results = c.list(searchClosure)
        c = Post.createCriteria()
        def resultCount = c.count(searchClosure1)

 是不是grails2.2的问题?

oksonic 2013-01-11

def index() {
        def isFirstResult = true
        def searchClosure = {
            if (params) {
                eq('status', PostStatus.VERIFIED_SUCCESS);
                // 按标签查询
                if (params.tags) {
                    eq('tags', params.tags);
                }
                if (params.keyword){
                    or {
                        like("title","%${params.keyword}%")
                        like("brief","%${params.keyword}%")
                    }
                }
                if (params.postTime) {
                    DateTime beginDate = new DateTime(params.postTime + '-01');
                    DateTime endDate = beginDate.plus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay)
                    ge("postTime", DateUtil.stringToDate(beginDate.toString()))
                    le("postTime", DateUtil.stringToDate(endDate.toString()))
                }
            }
            if (isFirstResult){
                maxResults(params.max?params.max.toInteger():paramsService.pageMax)
                firstResult(params.max?params.offset.toInteger():0)
                order("id", "desc")
            }
        }
        def c = Post.createCriteria()
        def results = c.list(searchClosure)
        isFirstResult = false
        c = Post.createCriteria()
        def resultCount = c.count(searchClosure)
        [postInstanceList: results, postInstanceTotal: resultCount, groupList: groupClass(), dateList: dateList()]
    }
 用这样的方法解决
Global site tag (gtag.js) - Google Analytics