ExtJS远程数据-本地分页

本文介绍了如何使用ExtJS 4的扩展类库Ext.ux.data.PagingStore实现本地分页。在后台不参与分页的情况下,前端通过添加时间戳强制刷新数据。同时,文中探讨了如何在页面加载后隐藏分页按钮中的“刷新”功能,提供了一种在Controller层利用afterrender事件处理的方法。
摘要由CSDN通过智能技术生成

背景

    一般情况下,分页展示是前端只负责展示,后台通过SQL语句实现分页查询。当总数据量在千条以下,适合一次性查询出符合条件的所有数据,让前端页面负责分页也是一种选择。

实例

    现通过ExtJS 4扩展类库Ext.ux.data.PagingStore来实现分页,建议使用前在GitHub获取最新版本。

    使用时非常简单,只需将Store的继承类改为“Ext.ux.data.PagingStore”,其他分页配置可参照之前的文章《ExtJS实现分页grid paging》。

 
 
 
  1. Ext.define('XXX', {
  2.  extend : 'Ext.ux.data.PagingStore'
  3.  ...
  4. })

    但是,针对不同的应用场景还有2个疑问:

  • 本地分页后,如何强制重新查询后台数据?

    根据PagingStore的实现来看,只有查询参数修改后才会再次调用后台进行查询。但是,如果我们修改了列表中某条数据后,需要按当前条件刷新列表。这时,我是在条件中添加一个时间戳来进行刷新的。

  1.    
       
       
    1. store.getProxy().extraParams._TIME=new Date().getTime();


  • 分页按钮中的“刷新”如何去除?

        因为是本地分页,ExtJS自带的分页“刷新”按钮似乎成了摆设。可以在页面加载完成后,将其隐藏。在Controller层添加afterrender事件来实现。代码中的tab_id可通过开发人员工具在ExtJS生成的页面源码中看到,这里是抛砖引玉,希望大家写出更好的选择器。

 
 
 
  1. afterrender : function(){
  2.        Ext.get("tab_id").down(".x-tbar-loading").up(".x-btn").setVisible(false);
  3. }


    

    

附上Ext.ux.data.PagingStore.js的源码:

 
 
 
  1. /*
  2. * PagingStore for Ext 4 - v0.6
  3. * Based on Ext.ux.data.PagingStore for Ext JS 3, by Condor, found at
  4. * http://www.sencha.com/forum/showthread.php?71532-Ext.ux.data.PagingStore-v0.5
  5. * Stores are configured as normal, with whatever proxy you need for remote or local.  Set the
  6. * lastOptions when defining the store to set start, limit and current page.  Store should only
  7. * request new data if params or extraParams changes.  In Ext JS 4, start, limit and page are part of the
  8. * options but no longer part of params.
  9. * Example remote store:
  10. *     var myStore = Ext.create('Ext.ux.data.PagingStore', {
  11.             model: 'Artist',
  12.             pageSize: 3,
  13.             lastOptions: {start: 0, limit: 3, page: 1},
  14.             proxy: {
  15.               type: 'ajax',
  16.               url: 'url/goes/here',
  17.               reader: {
  18.                 type: 'json',
  19.                 root: 'rows'
  20.               }
  21.             }
  22.       });
  23. * Example local store:
  24. *    var myStore = Ext.create('Ext.ux.data.PagingStore', {
  25.            model: 'Artist',
  26.            pageSize: 3,
  27.            proxy: {
  28.              type: 'memory',
  29.              reader: {
  30.                type: 'array'
  31.              }
  32.            },
  33.            data: data
  34.      });
  35. * To force a reload, delete store.lastParams.
  36. */
  37. Ext.define('Ext.ux.data.PagingStore', {
  38. extend: 'Ext.data.Store',
  39. alias: 'store.pagingstore',
  40. destroyStore: function () {
  41. this.callParent(arguments);
  42. this.allData = null;
  43. },
  44. /**
  45. * Currently, only looking at start, limit, page and params properties of options.  Ignore everything
  46. * else.
  47. * @param {Ext.data.Operation} options
  48. * @return {boolean}
  49. */
  50. isPaging: function (options) {
  51. var me = this,
  52. start = options.start,
  53. limit = options.limit,
  54. page = options.page,
  55. currentParams;
  56. if ((typeof start != 'number') || (typeof limit != 'number')) {
  57. delete me.start;
  58. delete me.limit;
  59. delete me.page;
  60. me.lastParams = options.params;
  61. return false;
  62. }
  63. me.start = start;
  64. me.limit = limit;
  65. me.currentPage = page;
  66. var lastParams = this.lastParams;
  67. currentParams = Ext.apply({}, options.params, this.proxy ? this.proxy.extraParams : {});
  68. me.lastParams = currentParams;
  69. if (!this.proxy) {
  70. return true;
  71. }
  72. // No params from a previous load, must be the first load
  73. if (!lastParams) {
  74. return false;
  75. }
  76. //Iterate through all of the current parameters, if there are differences, then this is
  77. //not just a paging request, but instead a true load request
  78. for (var param in currentParams) {
  79. if (currentParams.hasOwnProperty(param) && (currentParams[param] !== lastParams[param])) {
  80. return false;
  81. }
  82. }
  83. //Do the same iteration, but this time walking through the lastParams
  84. for (param in lastParams) {
  85. if (lastParams.hasOwnProperty(param) && (currentParams[param] !== lastParams[param])) {
  86. return false;
  87. }
  88. }
  89. return true;
  90. },
  91. applyPaging: function () {
  92. var me = this,
  93. start = me.start,
  94. limit = me.limit,
  95. allData, data;
  96. if ((typeof start == 'number') && (typeof limit == 'number')) {
  97. allData = this.data;
  98. data = new Ext.util.MixedCollection(allData.allowFunctions, allData.getKey);
  99. data.addAll(allData.items.slice(start, start + limit));
  100. me.allData = allData;
  101. me.data = data;
  102. }
  103. },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值