Ext4更新store总记录数量

  如果ext4的store对象通过loadData方法加载数据时,而不是通过load方法,那么即使配置了reader对象的totalProperty也无法正常获取总的记录数,为0。这样如果需要分页的时候就比较麻烦了,获取不到总的记录数。


  注意:使用store的loadData方法加载的数据是JSON数组,不能是JSON对象。如

{total:100,data:[{name:'showbo',addr:'guilin'},{name:'showbo1',addr:'guilin1'}]}

这种数据loadData是无法加载的,需要加载的是这种数据类型的data键的内容

[{name:'showbo',addr:'guilin'},{name:'showbo1',addr:'guilin1'}]


  配置reader对象的totalProperty和root仅对load方法有效,对loadData无用,所以看Ext4的API时需要注意一些。loadData方法Ext4说明如下


loadData( Ext.data.Model[]/Object[] data, [Boolean append] )
Loads an array of data straight into the Store.Using this method is great if the data is in the correct format already (e.g. it doesn't need to be processed by a reader). If your data requires processing to decode the data structure, use a MemoryProxy instead.
Parameters
data : Ext.data.Model[]/Object[]
Array of data to load. Any non-model instances will be cast into model instances first.
append : Boolean (optional)
True to add the records to the existing records in the store, false to remove the old ones first.
Defaults to: false



  通过ext4的store getTotalCount方法,如下

    getTotalCount: function() {
        return this.totalCount || 0;
    }


  知道总记录数是通过totalCount变量存储的,所以我们只需要继承Ext.data.Store自定义一个数据存储对象,提供一个方法更新totalCount就行了。

  Ext4通过自定义store对象更新store总记录数量示例demo源代码如下

Ext.onReady(function () {
  var s='{"results":100,"data":[{"user_name":"郑正英","user_id":"10","ssyj":"39569.0100"},{"user_name":"王雪梅","user_id":"11","ssyj":"557619.9900"},{"user_name":"张丹青","user_id":"12","ssyj":"116000.0000"},{"user_name":"张海东","user_id":"13","ssyj":"1412742.9900"}]}';
  var o=Ext.decode(s,true);
  //======================自定义store,提供一个对外可以更新总记录数的方法
  Ext.define('Ext.data.uStore', {
    extend: 'Ext.data.Store',
    setTotalCount:function(totalNum){this.totalCount=totalNum;}
  });
  //======================
  var store = new Ext.data.uStore({//创建自定义store对象
    fields: [{ name: 'user_id' }, { name: 'user_name' }, { name: 'ssyj', type: "float"}]
  });         
  store.loadData(o.data);//loadData放加载数据
  alert(store.getTotalCount())//输出总记录数,为0
  alert(store.getCount());//当前store存储的数据量,输出4
  store.setTotalCount(o.results);//设置store的总记录数
  alert(store.getTotalCount())//输出100
});

 

加支付宝好友偷能量挖...


原创文章,转载请注明出处:Ext4更新store总记录数量

评论(0)Web开发网
阅读(824)喜欢(1)extjs开发技巧