【问题标题】:Grails ExtJS paginationGrails ExtJS 分页
【发布时间】:2012-03-24 07:35:35
【问题描述】:

我正在使用 ExtJS 3.3.1 和 Grails 2.0 在屏幕上进行分页,但它并没有像我预期的那样工作。 我按照此处发布的提示进行操作:Grails extJS grid paging
JS页面

paramNames: {start:'offset',limit:'max',sort:'sort',dir:'order'},
baseParams: {offset:0,max:10},

分页工具栏:

this.gridBBar = new Ext.PagingToolbar({
            pageSize : 10,
            store : this.gridStore,
            displayInfo : true,
            displayMsg  : 'Hiển thị {0} - {1} mục tìm được của {2} kết quả',
            emptyMsg : 'Không tìm thấy dữ liệu',
        });

控制器:

def result = Floor.createCriteria().list(
   max:params.int('max')?:100, 
   offset:params.int('offset')?:0
) 
render ([count:result.totalCount,data:result] as JSON)

但分页按钮(下一步)被禁用,因为商店只包含 10 件商品,没有更多可检索的商品。
当我将偏移量更改为 10 时:

paramNames: {start:'offset',limit:'max',sort:'sort',dir:'order'},
baseParams: {offset:10,max:10},

分页效果很好,除了一件奇怪的事情:网格总是显示接下来的 10 个结果(第一次点击的第 10-20 条记录,第 2 次点击的第 20-30 条记录),而不是当前的前 10 个结果。我不知道 ExtJs 和 Grails 结合的分页的正确用法是什么。如果您有此问题的经验,能否请您分享一些信息?
非常感谢。

【问题讨论】:

  • 你的 json 是什么样的?你可能想看看这个:shitmores.blogspot.com/2007/04/…
  • 返回的 JSON 为 {"count":10, "items":[{"id":1, "name" : "AA"}, {"id":2, "name" :“B”}]}。我看到语句 render ([count:result.totalCount,data:result] as JSON) 不正确,因为返回列表没有 getTotalCount() 方法或 totalCount 属性

标签: grails extjs pagination


【解决方案1】:

哦,我真幸运。我明白了!
基于以下 2 篇文章:
1.http://grails.1312388.n4.nabble.com/Find-Count-for-pagination-And-Objects-for-Criteria-td1368528.html

2.http://blog.jeffshurts.com/2010/04/grails-pagination-and-criteriabuilder/
我已经找到了对此的解释。因为 JSON 中返回的 count 属性是从 result.size() 中得到的,所以它总是等于 grid 的 store 的 PagingToolbar 的 pageSize,所以 store 会明白,没有任何结果可以检索,它会禁用导航按钮。
这里的关键是返回查询的真实总结果(不附加分页约束)。像往常一样,createCriteria().list {} 将返回一个 ArraysList。但是通过将分页参数传递给如下列表:(参考链接 1)

DomainClass.createCriteria().list(max : x, offset : y) { 
// not pass max : x, offset : y to here, inside the body
}

Grails 会将结果作为 PagedResultList 隐式返回(请参阅链接 2),并为我们提供 getTotalCount()。没有任何 Grails 的官方文档提到这个神奇的问题。 我的问题就解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    相关资源
    最近更新 更多