ExtJs getTotalCount()未返回存储中的记录数
ExtJs getTotalCount() not returning the number of records in the store
当我从控制器中调用一个函数,该函数以以下方式加载存储中的数据时,
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"
}
}
]
}
相关文章:
- 制作一个阻止存储在浏览器历史记录中的网页
- pubnub存储和播放历史记录功能
- ExtJs getTotalCount()未返回存储中的记录数
- 更新sencha touch 2中的存储记录值
- 如何读取mysql表记录并存储在javascript对象中
- 如何在网页上存储记录 (json) 并仅在“视图”上的记录上显示
- 检查本地存储中的记录是否在 1 分钟前添加
- 本地存储:记录和保存,不起作用
- 从 ExtJs 4.1 存储中删除记录时触发哪些事件
- 无法使用 store.getById() 从存储中读取模型记录
- 本地存储循环删除 2 条记录后缺少记录(在不同的页面上)
- 为什么我得到一个空的记录存储
- 记录链接单击,然后保存到本地存储
- EXT JS :在组合框中,如何为不在存储中的记录设置显示字段
- 如何同步记录而不是下载整个数据存储
- 如果在此期间没有记录更改,则在两秒钟后同步存储
- 您如何解决 ExtJS 4.2 缓冲存储中的查找记录问题
- Extjs 4 存储 - 查询整个存储,而不仅仅是缓存的记录
- 如何使用本地存储存储用户的页面历史记录
- 以何种格式记录/存储数字数据以供进一步的JavaScript处理