ExtJS: return total rows/records in json store

17,292

Solution 1

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

Solution 2

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

{
    "total": 9999,
    "success": true,
    "users": [
        {
            "id": 1,
            "name": "Foo",
            "email": "[email protected]"
        }
    ]
}

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

Solution 3

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

Solution 4

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()
  }
});
Share:
17,292

Related videos on Youtube

Grigor
Author by

Grigor

Updated on June 04, 2022

Comments

  • Grigor
    Grigor almost 2 years

    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 problem?