开发者

ExtJS: return total rows/records in json store

开发者 https://www.devze.com 2023-03-24 02:02 出处:网络
I have a json store that returns values in json format. Now I need to get the number of rows/records in the json string but when I use store.getCount() function it returns 0, but the combobox is popul

I have a json store that returns values in json format. Now I need to get the number of rows/records in the json string but when I use store.getCount() function it returns 0, but the combobox is populated with rows, and when I use store.length I get undefined, probably because its not an array anymore, its returning from store, which is calling php script. Anyways, whats the best approach for this pro开发者_JS百科blem?


Try this out:

var myStore = Ext.extend(Ext.data.JsonStore, {
  ... config...,
  count : 0,
  listeners : {
    load : function(){
      this.count = this.getCount();
  }
}

Ext.reg('myStore', myStore);

and then use inside panels:

items : [{
 xtype : 'myStore',
 id : 'myStoreId'
}]

Whenever you need to get the count then you can simply do this:

Ext.getCmp('myStoreId').count


Your Json response from server, can be something like this...

{
    "total": 9999,
    "success": true,
    "users": [
        {
            "id": 1,
            "name": "Foo",
            "email": "foo@bar.com"
        }
    ]
}

Then you can use reader: { type : 'json', root : 'users', totalProperty : 'total', successProperty: 'success' } in your store object.


As from docs if your data source provided you can call getTotalCount to get dataset size.


If you use ajax proxy for the store, smth like

proxy : {
   type : 'ajax',
   url : 'YOUR URL',
   reader : {
       type : 'json',
       root : 'NAME OF YOUR ROOT ELEMENT',
       totalProperty : 'NAME OF YOUR TOTAL PROPERTY' // requiered for paging
   }
}

and then load your store like store.load(); There will be sent Ajax asynchronous request, so you should check count in callback like this

store.load({
  callback : function(records, operation, success) {
       console.log(this.getCount());         // count considering paging
       console.log(this.getTotalCount());    // total size
         // or even 
       console.log(records.length);          // number of returned records = getCount()
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消