如何在Grails的List中设置params max和offset

时间:2017-09-18 11:29:24

标签: grails gorm gsp

我被困在我从域中获取列表并将其保存到另一个列表中的位置。如何将param传递给新创建的列表

代码如下所示:

 params.max = Math.min(max ?: 10, 100)
        params.offset = params.offset as Integer ?: 0



        Calendar date = Calendar.getInstance();
        date.set(Calendar.DAY_OF_MONTH, 1);
        Calendar date2 = Calendar.getInstance();
        date2.set(Calendar.DAY_OF_MONTH, date.getActualMaximum(Calendar.DAY_OF_MONTH));

        def MonthStartDate =date.getTime()
        def MonthEndDate =date2.getTime()
        int monthMaxDays = MonthEndDate - MonthStartDate + 1


        Calendar cal0 = Calendar.getInstance();
        cal0.set(Calendar.DAY_OF_MONTH,1);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH,1);
     def myNewList  = new ArrayList<DomainABCTYPE>()
            def perDayDates = new ArrayList<Date>()




            for(int i=0;i<monthMaxDays;i++) {
                Date date1 = cal0.getTime();
                cal.add(Calendar.DATE, 1);
                Date calDate = cal.getTime();


              def myListPerDay = DomainABCTYPE.findAllByStartDateLessThanAndEndDateGreaterThanEquals(calDate, date1, params)



                int k=0
                for(;k < myListPerDay .size(); k++){
                    myNewList.add(myListPerDay .get(k))
                    perDayDates.add(date1)
                    }


                cal0.add(Calendar.DATE, 1)


            }


            render(view: "index", model:[ListInstance:myNewList ,ListCount:myNewList.size(), DatesList:perDayDates, MonthStartDate: MonthStartDate, MonthEndDate: MonthEndDate])

        }

1 个答案:

答案 0 :(得分:0)

Have a look here关于如何将分页参数直接传递给GORM查找方法。

我一直在简短地查看你的代码所有看起来很疯狂,你正在计算一个日期差异,然后迭代日期差异以找到记录。

为什么不写HQL query并使用between

where a.date BETWEEN :date1 AND :date2

总之,所有这些都可以通过1次查找来完成,并且不需要进行嵌套迭代查找