javascript ExtJS:返回 json 存储中的总行数/记录数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6878143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ExtJS: return total rows/records in json store
提问by Grigor
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?
我有一个 json 存储,它以 json 格式返回值。现在我需要获取 json 字符串中的行数/记录数但是当我使用store.getCount()
函数时它返回 0,但是组合框填充了行,当我使用时store.length
我得到未定义,可能是因为它不再是一个数组,它返回来自商店,它正在调用 php 脚本。无论如何,解决这个问题的最佳方法是什么?
采纳答案by Varun Achar
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
回答by Mark Huk
回答by perfect14
If you use ajax proxy for the store, smth like
如果您为商店使用 ajax 代理,就像
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() 一样加载你的商店;将发送 Ajax 异步请求,因此您应该像这样在回调中检查计数
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()
}
});
回答by toopay
Your Json response from server, can be something like this...
您来自服务器的 Json 响应,可能是这样的......
{
"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.
然后你可以reader: {
type : 'json',
root : 'users',
totalProperty : 'total',
successProperty: 'success'
}
在你的商店对象中使用。