我有这样的JSON。
{
"children":[
{"name": "subdivision1","id": "subdiv1","type": "subdiv","children":[
{"name": "Scheme1","id": "scheme1","type": "scheme","children":[
{"name": "Nathuji","id": "nathuji","type": "datalogger","leaf":true},
{ "name": "Tarsang","id": "tarsang","type": "datalogger","leaf":true},
{ "name": "Mithapur","id": "mithapur","type": "datalogger","leaf":true},
{ "name": "Samali","id": "samali","type": "datalogger","leaf":true}]},
{"name": "Scheme2","id": "scheme2","type": "scheme","children":[
{ "name": "Bhunidra","belongsto": "scheme2","id": "bhunidra","type": "datalogger","leaf":true},
{ "name": "Chhogala","belongsto": "scheme2","id": "chhogala","type": "datalogger","leaf":true}]
}]},
{"name": "subdivision2","id": "subdiv2","type": "subdiv","children":[
{"name": "Scheme3","id": "scheme3","type": "scheme","children":[
{ "name": "Thana Savli","belongsto": "scheme3","id": "thanasavli","type": "datalogger","leaf":true},
{ "name": "Padardi","belongsto": "scheme3","id": "padardi","type": "datalogger","leaf":true}]
}]}]}
这样的模特。
Ext.define('DemoApp.model.dataloggerhierarchymodel',{
extend: 'Ext.data.Model',
fields: ['name','id','type'],
proxy: {
type: 'ajax',
api: {
read: 'data/allschemes.json'
},
reader: {
type: 'json',
root: 'children'
}
}
});
这样存储。
Ext.define('DemoApp.store.dataloggerhierarchystore',{
extend: 'Ext.data.TreeStore',
model: 'DemoApp.model.dataloggerhierarchymodel',
autoLoad: true,
listeners: {
load:function(){
console.log('Datalogger Tree store loaded');
}
},
root: {
expanded: true
}
});
我在控制器中访问它,如
var schemesStore = this.getDataloggerhierarchystoreStore();
if(schemesStore == null || schemesStore == undefined){
console.log('Store Undefined');
}
for(var i = 0; i< schemesStore.count();i++)
{
console.log('Record - '+schemesStore[i].get('id'));
};
schemesStore.each(function(currentRecord){
console.log(currentRecord.get('id'));
});
我在树状面板中显示它,它工作正常。现在我想从商店访问每条记录。
但我收到错误count()
不是一个函数。在totalCount()
,each()
的情况下也是如此。我在这里错过了什么?如何从商店访问个人记录?
答案 0 :(得分:2)
不幸的是,Ext.data.TreeStore
不是Ext.data.Store
的子类。它没有普通商店所做的所有方法。要访问数据,您必须使用getRootNode()
并使用Ext.data.NodeInterface
中定义的方法访问根节点。
NodeInterface提供cascadeBy()
方法作为each()
的等效方法,您必须使用它来编写自己的count()
方法。