Ext.data.Store getTotalCount()在加载后不计算

时间:2013-03-07 08:44:14

标签: extjs extjs4 store

我的商店在调用getTotalCount()时并不总是返回正确数量的记录。我load()商店后出现此问题。我知道在那个检查点,商店里有记录 我正在使用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

如果商店包含所有数据,如何获取可以加载到商店的记录数量?


我的商店配置。

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
            })

我可以确认我的所有请求都发送了total属性。

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

1 个答案:

答案 0 :(得分:8)

Load是异步的。当你调用它时,商店会删除总计数属性,当你加载最多两个行后,服务器还没有返回更新属性:

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

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

你应该写:

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