grails - 如何在我的分页 View 中正确显示列表项

标签 grails controller pagination views

该 Controller 将列出我数据库中的所有游戏。

def index() {
    def platform = gameService.listPlatform()
    def max = params.max ?: 10
    def offset = params.offset ?: 0
    def chosenPlatfrm = params.platform
    def taskList = gameService.listGamePlat(chosenPlatfrm,max,offset)
    def taskL = gameService.whatsHot(chosenPlatfrm,max,offset)
    [games:taskL, bb:taskList, chosenPlatform:chosenPlatfrm, platforms:platform, gameCount:taskL.totalCount, gameCont:taskList.totalCount] 

}

这就是我的index.gsp的样子。
<g:each in="${games}" status="i" var="game">
        <g:if test="${game.averageRating != 0 }">
            <g:link action="gameProfile"
                params="${[gameTitle: "${game.gameTitle}"]}">
            </g:link>
        </g:if>
</g:each>
<div class="pagination" style="text-align: center;">
        <g:paginate action="index" total="${gameCount}" offset="0" max="3" params="${params.max}"/>
</div>

我想将分页 View 限制为每页仅3个项目。当index.gsp从我的索引 Controller 接收数据时,即使我有一个分页标签,它也会显示列表中的所有内容,但是当我单击一个页码时,它似乎运行良好。

我在索引 Controller 中尝试重定向,但显示错误“错误重定向循环”
 redirect(action: "index", params:[id:'',offset: '0', max: '3'])

如何正确显示index.gsp?

最佳答案

我认为存在一个问题。尝试:

<g:paginate total="${gameCount}" />

代替:
<g:paginate action="index" total="${gameCount}" offset="0" max="3" params="${params.max}"/>     

Controller 中的另一个问题是:您应该编写另一个函数来计算 gameCount 变量,该变量将计算每个分页 View 中所有游戏的数量(因此不包含offset和limit参数)。

当然,重定向是错误的,因为您要一遍又一遍地执行 Controller 的index()函数,因此请保留第一个提供的代码。

关于grails - 如何在我的分页 View 中正确显示列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34913584/

相关文章:

url - Grails/Groovy:URL参数(最大值,偏移量)空白/字符串时引发NumberFormatException

unit-testing - Grails Controller 测试 - 动态方法的问题

javascript - Controller 中的函数未被调用

java - 验证 Spring Controller 参数不为空

mongodb - 使用限制时使用 MongoDB 获取总文档数

grails - Grails在运行时更改数据源(JDBC URL)

unit-testing - 在 Spock 中模拟父类(super class)

mysql - 将 HQL 与 MySQL 结合使用,如何在分组依据之前对结果集进行排序,以便选择正确的记录?

java - 当用户操作可以删除中间的行时,如何处理数据库分页?

javascript - Angular 1.x - 无法通过动态数据检索实现分页