ExtJs getTotalCount()未返回存储中的记录数

ExtJs getTotalCount() not returning the number of records in the store

本文关键字:记录 存储 返回 getTotalCount ExtJs      更新时间:2023-09-26

当我从控制器中调用一个函数,该函数以以下方式加载存储中的数据时,

var store= new Ext.getStore('MyStore');
store.load();     

响应头显示返回以下JSON数据,

{"usrdata":[{"name":"John",
             "roll":"5",
             "address":"abcd"
           }]
}

但问题是当我写时

console.log(store.getTotalCount());

它在控制台上显示"0"(零)。

有人能帮我找出为什么它没有显示商店里记录数量的实际计数吗。

我觉得这可能是因为调用函数时存储尚未完成加载(可能是我不正确)。

以下是我的商店代码:

Ext.define('MyApp.store.MyStore', {
    extend: 'Ext.data.Store',
     model: 'MyApp.model.MyModel',
    timeout : 60000,
    proxy: {
        type: 'ajax',
        api: {
            create: 'php/insert.php', 
            read: 'php/read.php',
            update: 'php/update.php',
            destroy: 'php/delete.php',
        },
        reader: {
            type: 'json',
            root: 'usrdata',
            successProperty: 'success'
        },
        writer: {
            type: 'json',
            writeAllFields: true,
            encode: true,
            root: 'usrdata'
        }
    },
    autoLoad: true,
});

使用load方法中的回调来获取记录计数:http://docs.sencha.com/extjs/4.2.2/#/api/Ext.data.Store-方法加载

store.load({
    scope: this,
    callback: function(records, operation, success) {
        // the operation object
        // contains all of the details of the load operation
        console.log(records.length);
        // Alternative
        console.log(store.getTotalCount());
    }
});

您需要对商店定义进行微小更改

Ext.define('MyApp.store.MyStore', {
    extend: 'Ext.data.Store',
     model: 'MyApp.model.MyModel',
    timeout : 60000,
    proxy: {
        type: 'ajax',
        api: {
            create: 'php/insert.php', 
            read: 'php/read.php',
            update: 'php/update.php',
            destroy: 'php/delete.php',
        },
        reader: {
            type: 'json',
            root: 'usrdata',
            totalProperty : '<the_node_of_json_response_which_holds_the_count>'
            successProperty: 'success'
        },
        writer: {
            type: 'json',
            writeAllFields: true,
            encode: true,
            root: 'usrdata'
        }
    },
    autoLoad: true,
});

PS-您的服务需要返回计数。getTotalCount()不返回存储中包含的记录数,而是返回读取器对象的totalProperty所包含的数字。

因此,如果您的服务正在返回数据-

{
    "total": 122,
    "offset": 0,
    "users": [
        {
            "id": "ed-spencer-1",
            "value": 1,
            "user": {
                "id": 1,
                "name": "Ed Spencer",
                "email": "ed@sencha.com"
            }
        }
    ]
}

你的阅读器对象看起来像-

 reader: {
                type: 'json',
                root: 'usrdata',
                totalProperty : 'total'
                successProperty: 'success'
            },

以防万一我们解决了错误的问题。通常情况下,你会希望总计数器总是和你的数据一起从服务器返回,这样分页等都会自动工作。参见此处获取示例:

{
    "total": 122,
    "offset": 0,
    "users": [
        {
            "id": "ed-spencer-1",
            "value": 1,
            "user": {
                "id": 1,
                "name": "Ed Spencer",
                "email": "ed@sencha.com"
            }
        }
    ]
}