Search code examples
extjsextjs4store

Ext.data.Store getTotalCount() doesn't calculate after load


My store doesn't always return the right amount of records when calling getTotalCount(). This problem occurs after I load() the store. I know that there are records in the store at that point of checking.
I am using ExtJs 4.1.3

//this.grid = reference to my grid
var count = this.grid.getStore().getCount(), //50
    total = this.grid.getStore().getTotalCount(); //16000

    this.grid.getStore().load();

    count = this.grid.getStore().getCount(); //50
    total = this.grid.getStore().getTotalCount(); //0

How can I get the number of records that could be loaded into the Store if the Store contained all data?


My store configuration.

store: Ext.create('Ext.data.Store', {
                model: me.modelName,
                remoteSort: true,
                remoteFilter: true,
                pageSize: 50,
                trailingBufferZone: 25,
                leadingBufferZone: 50,
                buffered: true,
                proxy: {
                    type: 'ajax',
                    actionMethods: { read: 'POST' },
                    api: {
                        read: me.urls.gridUrl
                    },
                    extraParams: Ext.applyIf({ FilterType: 0 }, me.urlParams.gridUrlParams),
                    simpleSortMode: true,
                    reader: {
                        type: 'json',
                        root: 'data',
                        totalProperty: 'total'
                    }
                },
                autoLoad: true
            })

I can confirm that the total property is send for all of my requests.

{
    "succes": true,
    "data": [
    //50 records
    ],
    "total": 16219,
    "errors": []
}

Solution

  • Load is asynchronous. When you call it, the store deletes the total count property, and by the time you reach the two lines after load most chances the server hasn't returned yet to update the property:

    this.grid.getStore().load();
    
    // Server hasn't returned yet for these two lines.
    count = this.grid.getStore().getCount();
    total = this.grid.getStore().getTotalCount();
    

    You should really write:

    this.grid.getStore().load({
        scope: this,
        callback: function(records, operation, success) {
            count = this.getCount();
            total = this.getTotalCount();
        }
    });