【问题标题】:Grails pagination not workingGrails分页不起作用
【发布时间】:2013-11-28 00:00:03
【问题描述】:

我已经尝试解决这个分页问题一周了,但没有好的结果,

我的控制器里有这个

    PatientController{
    def show(Long id) {
    def patientInstance = Patient.get(id)
    if (!patientInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'patient.label', default: 'Patient'), id])
        return
    }



    def historyInstance = History.findAllByPatient(patientInstance)
    def total = historyInstance.size()


    [historyInstance: historyInstance,patientInstance: patientInstance,historyInstanceTotal: total]


    }
}

我在视图上有这段代码

    <g:each in="${historyInstance}" var="historyInstances" status="i">
      ...
    </g:each>

    <div class="pagination">
        <g:paginate  total="${historyInstanceTotal}"/>
    </div>

我使用了params&lt;g:paginate&gt; 上的最大值,我尝试了所有方法但没有结果,它总是在视图中显示整个历史记录。

【问题讨论】:

标签: grails gsp pagination


【解决方案1】:

您需要将您的params 传递给您正在使用的查找器,否则它将不知道如何分页。 &lt;g:paginate&gt; 标签仅用于显示分页链接,并不实际执行分页。此外,total 需要使用 countBy,因为结果集将被限制为当前页面的结果。

def historyInstance = History.findAllByPatient(patientInstance, params)
def total = History.countByPatient(patientInstance)

【讨论】:

  • 没有countAllBy,我试过了但显示错误,可能是另一种方式而不是findAllBy,我听说分页只适用于列表
  • findAllBylist 都返回域对象列表。
  • 当我得到计数​​时,historyInstance.countByPatient(patientInstance) 对我不起作用,但是,History.countByPatient(patientInstance) 做到了。
  • 感谢@Bernardo
猜你喜欢
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多