最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ExtJS: return total rowsrecords in json store - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question asked Jul 29, 2011 at 19:35 GrigorGrigor 4,04910 gold badges43 silver badges79 bronze badges 2
  • its an object, i tried .length and it didn't work – Grigor Commented Jul 29, 2011 at 22:07
  • How is your JsonStore getting loaded (proxy, store.loadData, inline data)? – Ballsacian1 Commented Jul 30, 2011 at 0:51
Add a comment  | 

4 Answers 4

Reset to default 5

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": "[email protected]"
        }
    ]
}

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)

  1. 暂无评论